PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

What makes a currency?

The currency consists of several informations. A name, a abbreviation and a sign. Each of these could be relevant to be displayed, but only one at the same time. It would not be a good practice to display something like "USD 1.000 $".

Therefor Zend_Currency supports the definition of the currency information which has to be rendered. The following constants can be used:

Table 44. Rendered informations for a currency

Constant Description
NO_SYMBOL No currency representation will be rendered at all
USE_SYMBOL The currency symbol will be rendered. For US Dollar this would be '$'
USE_SHORTNAME The abbreviation for this currency will be rendered. For US Dollar this would be 'USD'. Most abbreviations consist of 3 characters
USE_NAME The full name for this currency will be rendered. For US Dollar the full name would be "US Dollar"

Example 159. Selecting the currency description

Let's assume that your client has again set "en_US" as locale. Using no option the returned value could look like this:

<?php
$currency 
= new Zend_Currency(
    array(
        
'value' => 100,
    )
);

print 
$currency// Could return '$ 100'

By giving the proper option you can define what information which has to be rendered.

<?php
$currency 
= new Zend_Currency(
    array(
        
'value'   => 100,
        
'display' => Zend_Currency::USE_SHORTNAME,
    )
);

print 
$currency// Could return 'USD 100'

Without providing the display the currency sign will be used when rendering the object. When the currency has no sign, the abbreviation will be used as replacement.


Not all currencies have signs

You should note that not all currencies have default currency signs. This means, that when there is no default sign, and you set the sign to be rendered, you will not have a rendered currency at all because the sign is an empty string.

Sometimes it is necessary to change the default informations. You can set each of the three currency informations independently by giving the proper option. See the following example.

Example 160. Changing the currency description

Let's assume that your client has again set "en_US" as locale. But now we don't want to use the default settings but set our own description. This can simply be done providing the proper option:

<?php
$currency 
= new Zend_Currency(
    array(
        
'value' => 100,
        
'name'  => 'Dollar',
    )
);

print 
$currency// Could return 'Dollar 100'

You could also set a sign or an abbreviations yourself.

<?php
$currency 
= new Zend_Currency(
    array(
        
'value'    => 100,
        
'symbol' => '$$$',
    )
);

print 
$currency// Could return '$$$ 100'

Automatic display settings

When you set a name, abbreviation or sign yourself, than this new information will automatically be set to be rendered. This simplification prevents you from setting the proper display option when you set a information.

So using the sign option you can omit display and don't neet to setting it to 'USE_SYMBOL'.

Zend Framework