Skip to content


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 (May 23, 2008)

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

Posted in PHP, Programming.


9 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Tara says

    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 says

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

  3. alejandro says

    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 says

    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 says

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

  6. Jason says

    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.

  7. Mesbah Ahmed says

    Thanks a lot! Your hints saved me a lot of mid-night oil.

  8. Sergio Abreu says

    Thank you for posting this tutorial.
    Based on your code (thanks) I have created a function that could be use do anyone:
    /* currency function by Sergio Abreu */
    function currency( $number){
    setlocale(LC_ALL, ”);
    $locale = localeconv();
    return $locale['currency_symbol'] . ” ” . number_format($number, 2, $locale['decimal_point'], $locale['thousands_sep']);
    }

  9. Sergio Abreu says

    CORRECTING:
    Thank you for posting this tutorial.
    Based on your code (thanks) I have created a function that could be useful for all:
    /* currency function by Sergio Abreu */
    function currency( $number){
    setlocale(LC_ALL, ”);
    $locale = localeconv();
    return $locale['currency_symbol'] . ” ” . number_format($number, 2, $locale['decimal_point'], $locale['thousands_sep']);
    }



Some HTML is OK

or, reply to this post via trackback.