This RFC proposes extending the endianness modifier syntax (< and >) to floating-point format codes in PHP's pack() and unpack() functions.
Currently, PHP's pack/unpack functions support floating-point numbers with dedicated format letters:
f: single-precision float (4 bytes, machine byte order)d: double-precision float (8 bytes, machine byte order)g/G: single-precision float (little/big endian)e/E: double-precision float (little/big endian)
The new modifier syntax using < and > is not supported yet. Adding the modifier support to float formats would provide consistency with the endianness modifier syntax, recently accepted for integers in the upcoming 8.6.
This RFC presents an evolution mentioned in the “Future scope” section of the accepted RFC.
According to the Perl documentation (https://perldoc.perl.org/functions/pack), Perl supports endianness modifiers on floating-point format codes:
f< single-precision float, little-endian byte order f> single-precision float, big-endian byte order d< double-precision float, little-endian byte order d> double-precision float, big-endian byte order
This RFC proposes adding endianness modifiers to floating-point format codes:
f</f> for single-precision float (4-byte, little/big endian)d</d> for double-precision float (8-byte, little/big endian)Example usage:
<?php // little endian $data = pack('f<d<', 3.14159, 2.71828); // big endian $data = pack('f>d>', 3.14159, 2.71828); // Unpacking [$float, $double] = array_values(unpack('f<a/d<b', $data)); ?>
Equivalence with Existing Formats:
Modifiers on formats with inherent endianness (g/G and e/E) will emit a ValueError, for example:
<?php pack('g<', 3.14); // ValueError: Endianness modifier '<' cannot be applied to format code 'g' which already has inherent endianness pack('G>', 3.14); // ValueError: Endianness modifier '>' cannot be applied to format code 'G' which already has inherent endianness pack('e<', 3.14); // ValueError: Endianness modifier '<' cannot be applied to format code 'e' which already has inherent endianness pack('E>', 3.14); // ValueError: Endianness modifier '>' cannot be applied to format code 'E' which already has inherent endianness ?>
PHP’s floating-point pack/unpack formats operate on the platform’s native floating-point representation and size. In practice this is typically IEEE 754 with 32 bits float and 64 bits double. This RFC does not introduce a new floating-point representation, only a new syntax for already existing format code.
A possible next step to this RFC would be to deprecate e, E, g and G format letters. These letters are not supported by Perl's pack() and unpack() functions and become useless with the introduction of the endianness modifier syntax.
None. The modifier syntax is opt-in and does not affect existing format strings.
PHP 8.x (next minor)
Yes / No vote requiring 2/3 Yes votes to pass.