PHP pack()Functions

PHP Misc 参考手册PHP Misc Reference Manual

Examples

Pack data into a binary string:

<?php
echo pack("C3",80,72,80);
?>

Run Example »

Definition and Usage

The pack() function packs data into a binary string.

This is very useful in network transmission, file processing, and data storage, especially when precise control over data format and byte order is needed.


Syntax

string pack(string $format, mixed $arg1 [, mixed $arg2, ...])
  • $format: A string that specifies how the givenarg1, arg2, ... arguments are packed. Each character represents a data type and size.
  • $arg1, $arg2, ...: The data to be packed. Can be integers, floats, or strings, etc.

The pack() function returns the packed binary string, or false if an error occurs.


Parameters Description
format Required. Specifies the format used when packing data.

Possible values:

Integers:

  • c: signed char (1 byte).
  • C: unsigned char (1 byte).
  • s: signed short integer (2 bytes, host byte order).
  • S: unsigned short integer (2 bytes, host byte order).
  • l: signed long integer (4 bytes, host byte order).
  • L: unsigned long integer (4 bytes, host byte order).
  • q: signed long long integer (8 bytes, host byte order).
  • Q: unsigned long long integer (8 bytes, host byte order).

Floating point numbers:

  • f: single-precision floating point number (4 bytes, host byte order).
  • d: double-precision floating point number (8 bytes, host byte order).

Strings and other types:

  • x: NUL byte (padding byte, no argument).
  • a: NUL-terminated string (takes the argument as a string).
  • A: space-padded string (takes the argument as a string).
  • @: NUL bytes padded to a specified length.
args+ Optional. Specifies one or more arguments to be packed.

Technical Details

Return value: Returns a binary string containing the data.
PHP version: 4+
Changelog: The "Z" code was added in PHP 5.5 and has the same functionality as "a" used for Perl compatibility.


More Examples

Below are some examples using the pack() function.

Pack an unsigned short integer and a string:

Example

$data = pack('S', 65535); // Pack unsigned short integer 65535
echo bin2hex($data); // Output: ffff

Pack a signed long integer and a string:

Example

$data = pack('l', -2147483648); // Pack signed long integer -2147483648
echo bin2hex($data); // Output: 0000000080000000

Pack a float and a string:

Example

$data = pack('f', 3.14); // Pack float 3.14
echo bin2hex($data); // Output: 4048f5c3

Notes

  • pack()The function is a powerful tool for converting data into a binary format, but you need to ensure that you understand the meaning and usage of each character in the required format.
  • Endianness (byte order) issue: On different systems, integers and floating-point numbers may be represented differently. You can usepack()andunpack()the function to ensure correct transmission and parsing of data between different systems.
  • You can usebin2hex()the function to convert binary data into a hexadecimal string for output and debugging.
PHP Misc 参考手册PHP Misc Reference Manual Other Extensions