Definition and usage
pack()
function packages the data into a binary string.
Example
Example 1
Package the data into a binary string:
<?php
echo pack ( "C3" , 80 , 72 , 80 ) ;
?>
Try it yourself
Example explanation:
pack("C*", 80, 72, 80) Pack three unsigned characters (80, 72, 80) into a binary string. C* means packing one or more unsigned characters. In this example, the output will be a binary string containing these three characters. Note that characters 80, 72, and 80 correspond to characters 'P', 'H' and 'P' in the ASCII table, respectively, so the output will be the string "PHP".
Example 2
<?php
echo pack ( "C*" , 80 , 72 , 80 ) ;
?>
Try it yourself
grammar
pack ( format , args + )
parameter |
describe |
format
|
Required. Specifies the format used when packaging data. |
args+
|
Optional. Specifies one or more parameters to be packaged. |
Possible values for format parameter:
- a - NUL filled string
- A - SPACE filled string
- h - Hexadecimal string, low bit in front
- H - Hexadecimal string, high digits are in front
- c - Signed characters
- C - Unsigned characters
- s - signed short integer (always 16 bits, machine endian)
- S - Unsigned short integer (always 16-bit, machine-byte order)
- n - Unsigned short integer (always 16-bit, big-endian endian)
- v - Unsigned short integer (always 16-bit, little-endian)
- i - signed integers (machine-related size and endianness)
- I - Unsigned integers (machine-related size and endianness)
- l - signed long integer (always 32 bits, machine endian)
- L - Unsigned long integer (always 32 bits, machine endian)
- N - Unsigned long integer (always 32-bit, big-endian)
- V - Unsigned long integer (always 32 bits, little endian byte order)
- q - signed long integer (always 64 bits, machine endian)
- Q - Unsigned long integer (always 64 bits, machine endian)
- J - Unsigned long integer (always 64-bit, big-endian endian)
- P - Unsigned long integer (always 64-bit, little-endian)
- f - Floating point number (machine-related size and representation)
- g - floating point number (machine-related size, little endian endian endian)
- G - Floating point number (machine-related size, big-endian endian)
- d - Double precision floating point number (machine-related size and representation)
- e - Double precision floating point number (machine-related size, small-endian endian)
- E - Double precision floating point number (machine-related size, big-endian endian)
- x - NUL bytes
- X - Rewind one byte
- Z - NUL filled string
- @ - NUL Fill to absolute position