pack()
pack() — Pack data into binary string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Pack given arguments into a binary string according to format.
Signature
pack(string $format, mixed ...$values): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $format | string | The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are ... |
| ...$values | mixed |
Return value
Returns a binary string containing data.
Example
A runnable snippet:
$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);
Changelog
| Version | Change |
|---|---|
| 8.0.0 | This function no longer returns false on failure. |
| 7.2.0 | float and double types supports both Big Endian and Little Endian. |
| 7.0.15, 7.1.1 | The "e", "E", "g" and "G" codes were added to enable byte order support for float and double. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page pack() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.