Current Location: Home> Latest Articles> The money_format function is no longer supported in PHP 7 and above, how to replace or solve this issue?

The money_format function is no longer supported in PHP 7 and above, how to replace or solve this issue?

gitbox 2025-09-12

In PHP 7.4 and later versions, the money_format() function has been deprecated and is no longer supported. Developers now need to find alternative solutions for formatting currency values in PHP.

1. Replacing `money_format()` with `NumberFormatter`

One of the best alternatives to the `money_format()` function is the NumberFormatter class, which is part of the intl extension in PHP. This class allows for the formatting of numbers, including currency, in a way that respects local conventions.

Here is an example of how to use the NumberFormatter class to format currency:

<?php
$locale = 'en_US';
$currency = 'USD';
$amount = 123456.78;
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
echo $fmt->formatCurrency($amount, $currency);
?>

In this example, NumberFormatter not only formats the number but also automatically adds the currency symbol, thousands separator, and decimal point based on the locale. Developers only need to specify the appropriate locale and currency code.

2. Benefits of Using `NumberFormatter`

  • Cross-platform: It does not rely on the operating system's locale configuration, ensuring consistent formatting output.
  • Multi-language and multi-currency support: Easily supports various country and currency formats, making it suitable for international projects.
  • Rich functionality: It allows you to control the number of decimal places, symbol positioning, and other aspects of formatting, providing great flexibility.

3. Considerations

Before using NumberFormatter, make sure that the PHP environment has the intl extension enabled, as it is required for the class to work. Most modern PHP environments include this extension by default, but some lightweight environments may need to install and configure it manually.

4. Conclusion

As PHP continues to evolve, the money_format() function has been deprecated and should no longer be used. The NumberFormatter class is the recommended replacement, providing a more stable and cross-platform solution for formatting currency. It is highly recommended that new projects and ongoing projects migrate to NumberFormatter as soon as possible to ensure modernity and compatibility in the codebase.