Yesterday I finished up a project I was working on that I wrote in PHP. I created it on a Linux machine and I needed to test it on Windows. On the Windows system I tested it using the latest version of WAMP. I had it all setup ready to go when I got a nasty error message Fatal error: Call to undefined function money_format(). I thought it was rather strange but I figured that I didn’t have the correct PHP extension enabled. After looking for such a extension, I started to think about it more and realized that it wasn’t apart of an extension. I took a look at the manual for money_format and found that the function doesn’t exist on Windows.
Note: The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.
Since I was in a rush to get it up and running I copied this function for the time being. I started digging around for alternatives since I didn’t want to include that function in the beginning of my code. I wanted a solution that would work on both systems without any special work a rounds. The functions you need to use are number_format combined with localeconv. Here is an example.
<?php setlocale(LC_ALL, ''); // Locale will be different on each system. $amount = 1000000.97; $locale = localeconv(); echo $locale['currency_symbol'], number_format($amount, 2, $locale['decimal_point'], $locale['thousands_sep']); ?>
With this you can write code that is actually portable instead of relying on operating system features. Having the money_format function available in PHP without it being an extension is pretty stupid. I don’t see why you would want to create inconsistencies like this between different operating systems in a programming language. Especially since this seems to be apart of the standard base of functions included with PHP. Maybe PHP is as bad as they say it is.
Update (May 23, 2008)
I thought I would mix it up by creating several tiny articles instead of one large article today.