Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String localeconv() Function



The PHP String localeconv() function is used to get numeric formatting information. This function returns an array with local numeric and monetary formatting information. Since this method is non-parameterized, we do not have to provide anything.

Syntax

Below is the syntax of the PHP String localeconv() function −

array localeconv ( void )

Parameters

This function does not accepts any parameter.

Return Value

The localeconv() function returns data based upon the current locale as set by setlocale().

Array Elements

The returned array contains the following fields −

Array Element Description
decimal_point It is the decimal point character
thousands_sep It is the thousands separator
grouping It is an array containing numeric groupings
int_curr_symbol It is an international currency symbol (i.e. USD)
currency_symbol It is a local currency symbol (i.e. $)
mon_decimal_point It is the monetary decimal point character
mon_thousands_sep It is the monetary thousands separator
mon_grouping It is an array containing monetary groupings
positive_sign It is the sign for positive values
negative_sign It is the sign for negative values
int_frac_digits It is an international fractional digits
frac_digits It is the local fractional digits
p_cs_precedes It is true if currency_symbol precedes a positive value, false if it succeeds one
p_sep_by_space It is true if a space separates currency_symbol from a positive value, false otherwise
n_cs_precedes It is true if currency_symbol precedes a negative value, false if it succeeds one
n_sep_by_space It is true if a space separates currency_symbol from a negative value, false otherwise
p_sign_posn 0 - Parentheses surround the quantity and currency_symbol
1 - The sign string precedes the quantity and currency_symbol
2 - The sign string succeeds the quantity and currency_symbol
3 - The sign string immediately precedes the currency_symbol
4 - The sign string immediately succeeds the currency_symbol
n_sign_posn 0 - Parentheses surround the quantity and currency_symbol
1 - The sign string precedes the quantity and currency_symbol
2 - The sign string succeeds the quantity and currency_symbol
3 - The sign string immediately precedes the currency_symbol
4 - The sign string immediately succeeds the currency_symbol

PHP Version

First introduced in core PHP 4.0.5, the localeconv() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP String localeconv() function to show the default locale information, if there is no locale is set explicitly.

<?php
   // Retrieve locale-specific information
   $locale_info = localeconv();

   // Display the array with default locale information
   print_r($locale_info);
?> 

Output

This will generate the below output −

Array
(
   [decimal_point] => .
   [thousands_sep] => 
   [int_curr_symbol] => 
   [currency_symbol] => 
   [mon_decimal_point] => 
   [mon_thousands_sep] => 
   [positive_sign] => 
   [negative_sign] => 
   [int_frac_digits] => 127
   [frac_digits] => 127
   [p_cs_precedes] => 127
   [p_sep_by_space] => 127
   [n_cs_precedes] => 127
   [n_sep_by_space] => 127
   [p_sign_posn] => 127
   [n_sign_posn] => 127
   [grouping] => Array
      (
      )
   [mon_grouping] => Array
      (
      )
)

Example 2

Here we will show you how the localeconv() function is used to set the locale for the script to "US" and retrieves locale specific information.

<?php
   // Set the locale to United States 
   setlocale(LC_ALL,"US");

   // Retrieve locale-specific information
   $locale_info = localeconv();

   // Print the array containing locale-specific details
   print_r($locale_info);
?>

Output

Here is the outcome of the following code −

Array
(
   [decimal_point] => .
   [thousands_sep] => 
   [int_curr_symbol] => 
   [currency_symbol] => 
   [mon_decimal_point] => 
   [mon_thousands_sep] => 
   [positive_sign] => 
   [negative_sign] => 
   [int_frac_digits] => 127
   [frac_digits] => 127
   [p_cs_precedes] => 127
   [p_sep_by_space] => 127
   [n_cs_precedes] => 127
   [n_sep_by_space] => 127
   [p_sign_posn] => 127
   [n_sign_posn] => 127
   [grouping] => Array
      (
      )
   [mon_grouping] => Array
      (
      )
)

Example 3

Now the below code illustrates the formatting differences between the French (fr_FR) and US (en_US) locale settings with the help of localeconv() function.

<?php
   // Set and retrieve US locale
   setlocale(LC_ALL, "en_US");
   $us_locale = localeconv();

   // Set and retrieve French locale
   setlocale(LC_ALL, "fr_FR");
   $fr_locale = localeconv();

   // Display differences
   echo "US Decimal Point: " . $us_locale['decimal_point'].PHP_EOL;
   echo "French Decimal Point: " . $fr_locale['decimal_point'].PHP_EOL;
   echo "US Currency Symbol: " . $us_locale['currency_symbol'].PHP_EOL;
   echo "French Currency Symbol: " . $fr_locale['currency_symbol'].PHP_EOL;
?> 

Output

This will create the below output −

US Decimal Point: .
French Decimal Point: ,
US Currency Symbol: $
French Currency Symbol: Eu
php_function_reference.htm
Advertisements