Introduction to Zend_Currency | Zend_Currency Programmer's Reference Guide | Options for currencies

# Using Zend_Currency

# Generic usage

The simplest usecase within an application is to use the clients locale. When you create a instance of Zend_Currency without giving any options, your clients locale will be used to set the proper currency.

Example #1 Creating a currency with client settings

Let's assume that your client has set "en_US" as wished language within his browser. In this case Zend_Currency will automatically detect the currency which has to be used.

1. $currency = new Zend_Currency();
3. // See the default settings which are depending on the client
4. // var_dump($currency);

The created object would now contain the currency "US Dollar" as this is the actual assigned currency for US (United States). It has also other options set, like "$" for the currency sign or "USD" for the abbreviation.

> Note: Automatic locale detection does not always work
> You should note that this automatic locale detection does not always work properly. The reason for this behaviour is that Zend_Currency must have a locale which includes a region. When the client would only set "en" as locale Zend_Currency would not know which of the more than 30 countries was meant. In this case an exception would be raised.
> A client could also omit the locale settings within his browser. This would lead to the problem that your environment settings will be used as fallback and could also lead to an exception.

# Currency creation based on a locale

To prevent the problems with your client you could simply set the wished locale manually.

1. $currency = new Zend_Currency('en_US');
3. // You can also use the 'locale' option
4. // $currency = new Zend_Currency(array('locale' => 'en_US'));
6. // See the actual settings which are fixed to 'en_US'
7. // var_dump($currency);

As within our first example the used currency will be "US Dollar". But now we are no longer dependend on the clients settings.

Zend_Currency also supports the usage of an application-wide locale. You can set a Zend_Locale instance in the registry as shown below. With this notation you can avoid setting the locale manually for each instance when you want to use the same locale throughout the application.

1. // in your bootstrap file
2. $locale = new Zend_Locale('de_AT');
3. Zend_Registry::set('Zend_Locale', $locale);
5. // somewhere in your application
6. $currency = new Zend_Currency();

# Currency creation based on a country

Zend_Currency is also able to work on a given country by using Zend_Locale internally.

1. $currency = new Zend_Currency('US');
3. // See the actual settings which are fixed to 'en_US'
4. // var_dump($currency);

> Note: Uppercase territories
> When you know that you are using a territory, then you should uppercase it. Otherwise you could get an in your eyes false locale in return. For example, when you give "om" then you could expect "ar_OM" to be returned. But in fact it returns "om", as it's also a language.
> Therefor always uppercase the input when you know that a territory is meant.

Introduction to Zend_Currency | Zend_Currency Programmer's Reference Guide | Options for currencies