Problems like How to get an associative array using prepared queries with mysqli have already been fixed? when it is only available with mysqlnd
.
I do not understand why the complications of PHP
creating functions that are not available if they do not have drivers or libraries installed, it is assumed that if a server is installed it apache
should work as such.
Let's see the following usage requires having the Intl extension to be able to use:
- NumberFormatter::formatCurrency - Formats a currency value according to the formatter rules.
Example:
$amount = '12345.67';
$formatter = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
echo 'UK: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
$formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo 'DE: ', $formatter->formatCurrency($amount, 'EUR'), PHP_EOL;
Fatal error: Uncaught Error: Class 'NumberFormatter' not found in C:\xampp\htdocs\Edition\formatodemoneda.php:4 Stack trace: #0 {main} thrown
And finally the function money_format()
is only defined if the system has capacity strfmon
. For example, Windows
it doesn't, so it money_format()
's not defined in Windows
.
Example
$number = 1234.56;
// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56
Fatal error: Uncaught Error: Call to undefined function money_format() in C:\xampp\htdocs\Edition\formatodemoneda.php:21 Stack trace: #0 {main} thrown
In some similar questions on StackOcerflow English, it is observed that it tries to create dependent functions for the type of currency, for example a function for the format of Indian currency , among other failed attempts . But if there are several currencies, it doesn't seem like a solution to me to add so many functions for each type of currency.
Until I found this question Alternative to money_format() Function in PHP on Windows Platform with a very curious answer that could surely be used as a reference to create a solution like the one in mysqlnd
or have to resign yourself to giving formats using number-format
.
function money_format($floatcurr, $curr = 'EUR'){
$currencies['ARS'] = array(2, ',', '.'); // Argentine Peso
$currencies['AMD'] = array(2, '.', ','); // Armenian Dram
$currencies['AWG'] = array(2, '.', ','); // Aruban Guilder
$currencies['AUD'] = array(2, '.', ' '); // Australian Dollar
$currencies['BSD'] = array(2, '.', ','); // Bahamian Dollar
$currencies['BHD'] = array(3, '.', ','); // Bahraini Dinar
$currencies['BDT'] = array(2, '.', ','); // Bangladesh, Taka
$currencies['BZD'] = array(2, '.', ','); // Belize Dollar
$currencies['BMD'] = array(2, '.', ','); // Bermudian Dollar
$currencies['BOB'] = array(2, '.', ','); // Bolivia, Boliviano
$currencies['BAM'] = array(2, '.', ','); // Bosnia and Herzegovina, Convertible Marks
$currencies['BWP'] = array(2, '.', ','); // Botswana, Pula
$currencies['BRL'] = array(2, ',', '.'); // Brazilian Real
$currencies['BND'] = array(2, '.', ','); // Brunei Dollar
$currencies['CAD'] = array(2, '.', ','); // Canadian Dollar
$currencies['KYD'] = array(2, '.', ','); // Cayman Islands Dollar
$currencies['CLP'] = array(0, '', '.'); // Chilean Peso
$currencies['CNY'] = array(2, '.', ','); // China Yuan Renminbi
$currencies['COP'] = array(2, ',', '.'); // Colombian Peso
$currencies['CRC'] = array(2, ',', '.'); // Costa Rican Colon
$currencies['HRK'] = array(2, ',', '.'); // Croatian Kuna
$currencies['CUC'] = array(2, '.', ','); // Cuban Convertible Peso
$currencies['CUP'] = array(2, '.', ','); // Cuban Peso
$currencies['CYP'] = array(2, '.', ','); // Cyprus Pound
$currencies['CZK'] = array(2, '.', ','); // Czech Koruna
$currencies['DKK'] = array(2, ',', '.'); // Danish Krone
$currencies['DOP'] = array(2, '.', ','); // Dominican Peso
$currencies['XCD'] = array(2, '.', ','); // East Caribbean Dollar
$currencies['EGP'] = array(2, '.', ','); // Egyptian Pound
$currencies['SVC'] = array(2, '.', ','); // El Salvador Colon
$currencies['ATS'] = array(2, ',', '.'); // Euro
$currencies['BEF'] = array(2, ',', '.'); // Euro
$currencies['DEM'] = array(2, ',', '.'); // Euro
$currencies['EEK'] = array(2, ',', '.'); // Euro
$currencies['ESP'] = array(2, ',', '.'); // Euro
$currencies['EUR'] = array(2, ',', '.'); // Euro
$currencies['FIM'] = array(2, ',', '.'); // Euro
$currencies['FRF'] = array(2, ',', '.'); // Euro
$currencies['GRD'] = array(2, ',', '.'); // Euro
$currencies['IEP'] = array(2, ',', '.'); // Euro
$currencies['ITL'] = array(2, ',', '.'); // Euro
$currencies['LUF'] = array(2, ',', '.'); // Euro
$currencies['NLG'] = array(2, ',', '.'); // Euro
$currencies['PTE'] = array(2, ',', '.'); // Euro
$currencies['GHC'] = array(2, '.', ','); // Ghana, Cedi
$currencies['GIP'] = array(2, '.', ','); // Gibraltar Pound
$currencies['GTQ'] = array(2, '.', ','); // Guatemala, Quetzal
$currencies['HNL'] = array(2, '.', ','); // Honduras, Lempira
$currencies['HKD'] = array(2, '.', ','); // Hong Kong Dollar
$currencies['HUF'] = array(0, '', '.'); // Hungary, Forint
$currencies['ISK'] = array(0, '', '.'); // Iceland Krona
$currencies['INR'] = array(2, '.', ','); // Indian Rupee
$currencies['IDR'] = array(2, ',', '.'); // Indonesia, Rupiah
$currencies['IRR'] = array(2, '.', ','); // Iranian Rial
$currencies['JMD'] = array(2, '.', ','); // Jamaican Dollar
$currencies['JPY'] = array(0, '', ','); // Japan, Yen
$currencies['JOD'] = array(3, '.', ','); // Jordanian Dinar
$currencies['KES'] = array(2, '.', ','); // Kenyan Shilling
$currencies['KWD'] = array(3, '.', ','); // Kuwaiti Dinar
$currencies['LVL'] = array(2, '.', ','); // Latvian Lats
$currencies['LBP'] = array(0, '', ' '); // Lebanese Pound
$currencies['LTL'] = array(2, ',', ' '); // Lithuanian Litas
$currencies['MKD'] = array(2, '.', ','); // Macedonia, Denar
$currencies['MYR'] = array(2, '.', ','); // Malaysian Ringgit
$currencies['MTL'] = array(2, '.', ','); // Maltese Lira
$currencies['MUR'] = array(0, '', ','); // Mauritius Rupee
$currencies['MXN'] = array(2, '.', ','); // Mexican Peso
$currencies['MZM'] = array(2, ',', '.'); // Mozambique Metical
$currencies['NPR'] = array(2, '.', ','); // Nepalese Rupee
$currencies['ANG'] = array(2, '.', ','); // Netherlands Antillian Guilder
$currencies['ILS'] = array(2, '.', ','); // New Israeli Shekel
$currencies['TRY'] = array(2, '.', ','); // New Turkish Lira
$currencies['NZD'] = array(2, '.', ','); // New Zealand Dollar
$currencies['NOK'] = array(2, ',', '.'); // Norwegian Krone
$currencies['PKR'] = array(2, '.', ','); // Pakistan Rupee
$currencies['PEN'] = array(2, '.', ','); // Peru, Nuevo Sol
$currencies['UYU'] = array(2, ',', '.'); // Peso Uruguayo
$currencies['PHP'] = array(2, '.', ','); // Philippine Peso
$currencies['PLN'] = array(2, '.', ' '); // Poland, Zloty
$currencies['GBP'] = array(2, '.', ','); // Pound Sterling
$currencies['OMR'] = array(3, '.', ','); // Rial Omani
$currencies['RON'] = array(2, ',', '.'); // Romania, New Leu
$currencies['ROL'] = array(2, ',', '.'); // Romania, Old Leu
$currencies['RUB'] = array(2, ',', '.'); // Russian Ruble
$currencies['SAR'] = array(2, '.', ','); // Saudi Riyal
$currencies['SGD'] = array(2, '.', ','); // Singapore Dollar
$currencies['SKK'] = array(2, ',', ' '); // Slovak Koruna
$currencies['SIT'] = array(2, ',', '.'); // Slovenia, Tolar
$currencies['ZAR'] = array(2, '.', ' '); // South Africa, Rand
$currencies['KRW'] = array(0, '', ','); // South Korea, Won
$currencies['SZL'] = array(2, '.', ', '); // Swaziland, Lilangeni
$currencies['SEK'] = array(2, ',', '.'); // Swedish Krona
$currencies['CHF'] = array(2, '.', '\''); // Swiss Franc
$currencies['TZS'] = array(2, '.', ','); // Tanzanian Shilling
$currencies['THB'] = array(2, '.', ','); // Thailand, Baht
$currencies['TOP'] = array(2, '.', ','); // Tonga, Paanga
$currencies['AED'] = array(2, '.', ','); // UAE Dirham
$currencies['UAH'] = array(2, ',', ' '); // Ukraine, Hryvnia
$currencies['USD'] = array(2, '.', ','); // US Dollar
$currencies['VUV'] = array(0, '', ','); // Vanuatu, Vatu
$currencies['VEF'] = array(2, ',', '.'); // Venezuela Bolivares Fuertes
$currencies['VEB'] = array(2, ',', '.'); // Venezuela, Bolivar
$currencies['VND'] = array(0, '', '.'); // Viet Nam, Dong
$currencies['ZWD'] = array(2, '.', ' '); // Zimbabwe Dollar
// custom function to generate: ##,##,###.##
function formatinr($input)
{
$dec = "";
$pos = strpos($input, ".");
if ($pos === FALSE)
{
//no decimals
}
else
{
//decimals
$dec = substr(round(substr($input, $pos), 2), 1);
$input = substr($input, 0, $pos);
}
$num = substr($input, -3); // get the last 3 digits
$input = substr($input, 0, -3); // omit the last 3 digits already stored in $num
// loop the process - further get digits 2 by 2
while (strlen($input) > 0)
{
$num = substr($input, -2).",".$num;
$input = substr($input, 0, -2);
}
return $num.$dec;
}
if ($curr == "INR")
{
return formatinr($floatcurr);
}
else
{
return number_format($floatcurr, $currencies[$curr][0], $currencies[$curr][1], $currencies[$curr][2]);
}
}
An easy way to tell the function
number_format
what kind of format to output is to use the colon operator (PHP 5.6+), called Variable Length Argument List or Argument Unpacking Operator (...) also used dynamically in abind_param($variable, ...$array);
.There is another function called
call_user_func_array()
a usage example:Other references on StackOverflow What is the meaning of Three dots (...) in PHP? .
And finally we get a certain part of the code, and we tell the function
number_format
what kind of coin to generate using the variable$curr
.$curr = 'USD';
Another way to use without defining the type of currency in the variable is to do it from our functionmoneyFormat
in this way:Indeed, first to clarify why PHP requires other libraries.
In general, a programming language includes a series of basic functions and libraries for generic use, but others are used from third-party sources ( not always compatible with all operating systems ) .
In many cases it doesn't make sense to reimplement the library in pure PHP instead of using a pre-existing solution or function of the operating system ( besides it wouldn't be as optimal ), for example:
ffmpeg
tool to treat videos, to be able to use a function and to work with video editing it is necessary that the server's operating system has this tool installed or to manage IMAP mail it is necessary to install the extensionphp5-imap
.In many cases, when installing the interpreter or compiler of the language, it does not install packages or libraries that it considers to be of infrequent use, so an alternative is to check the documentation to validate if this is not a compilation option: For example,
php_zip
to be able to use it is need to compile php using argument--enable-zip
https://www.php.net/manual/en/zip.installation.phpIn the case of your specific question, dates are a controversial topic in technology issues since they have many representations and depend on the configuration of the server, I understand that methods such as
money_format
can count on the use of some function of the operating system to consult which is the locale of the server.Why doesn't Apache solve this for me?
Apache only interacts with PHP as a web server which receives the requests, outsources its execution with PHP and returns a response to the user, in versions 7.x of PHP Apache is not necessary, so as not to create a dependency that is difficult to break between the web server and the language is that it does not provide complementary libraries or dependencies, besides that it would escape its role.
So what do I do with formatCurrency?
For now, effectively, as you comment, the solutions that exist are:
auto_prepend_file
in php.ini