PHP 8.0: New %h and %H printf specifiers

Version8.0
TypeNew Feature

PHP 8.0 adds new %h and %H format specifiers for printf class of functions. They work similar to the existing %g and %G modifiers, but are locale-independent.

printf('%h', 3.141592653589793238); // 3.14159
printf('%H', 3.141592653589793238); // 3.14159

Prior to PHP 8.0, float to string conversions were locale-aware. This is changed in PHP 8.0. The existing %g and %G specifiers remain locale-aware, and the new %h and %H are their locale-independent counterparts.

setlocale(LC_ALL, "fr_FR");

// Locale-aware, with fr_FR comma decimal separator
printf('%g', 3.141592653589793238); // 3,14159
printf('%G', 3.141592653589793238); // 3,14159

// Locale-independent
printf('%h', 3.141592653589793238); // 3.14159
printf('%H', 3.141592653589793238); // 3.14159

Backwards Compatibility Impact

If %h and %H specifiers are used in PHP versions prior to 8.0, they will be processed to empty strings without raising any errors.

Related Changes


Implementation