smert.net

Polluting the Internet one post at a time…

Inconsistencies Between PHP on Windows (Show Me the Money_Format)

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 (23MAY08)

I thought I would mix it up by creating several tiny articles instead of one large article today.

6 Comments so far

  1. Tara May 24th, 2008 12:50 pm

    Jason! I would love you forever if you could fix the link to the fluff friends list! I get rick rolled everytime! LOL I used to have it, but cant find it on my HD. Pretty please???

    Thanks <3

  2. Jason May 24th, 2008 2:08 pm

    Save your love for Carl. Anyways, here is the list

  3. alejandro September 27th, 2008 9:34 pm

    i have a big problem , i ve just buy a shop cart application who has bben developen in php but is encrypted with ion cube, the problem is tthat my server is a windows server and i have a money_format function that sends me a n error message .

    i want to fix this but i cannot change the code because i have ion cube

    is there a way to fix the php windows library to accept money_format?

  4. Jason September 29th, 2008 5:04 pm

    I would contact the author of the shopping cart and see if they have a fix for that issue. Unless you find a way to un-obfuscate the Ion Cube encoding there isn’t anything you can do.

  5. Rick March 2nd, 2009 4:11 am

    I would personally use $locale['mon_decimal_point'] and $locale['mon_thousands_sep'] - very useful example, thanks.

  6. Jason May 14th, 2009 8:51 pm

    I think that I should have used that instead. I wonder if there are any countries that have different separators for mon_thousands_sep and thousands_sep.

Leave a reply

Firefox 2