PHP Reference

Quick Reference

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

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

Output

PHP

Syntax

pack(format, args+)

Parameters

ParameterDescription
formatSpecifies the format to use when packing data:

  • a - NUL-padded string

  • A - SPACE-padded string

  • h - Hex string, low nibble first

  • H - Hex string, high nibble first

  • c - signed char

  • C - unsigned char

  • s - signed short (always 16 bit, machine byte order)

  • S - unsigned short (always 16 bit, machine byte order)

  • n - unsigned short (always 16 bit, big endian byte order)

  • v - unsigned short (always 16 bit, little endian byte order)

  • i - signed integer (machine dependent size and byte order)

  • I - unsigned integer (machine dependent size and byte order)

  • l - signed long (always 32 bit, machine byte order)

  • L - unsigned long (always 32 bit, machine byte order)

  • N - unsigned long (always 32 bit, big endian byte order)

  • V - unsigned long (always 32 bit, little endian byte order)

  • q - signed long long (always 64 bit, machine byte order)

  • Q - unsigned long long (always 64 bit, machine byte order)

  • J - unsigned long long (always 64 bit, big endian byte order)

  • P - unsigned long long (always 64 bit, little endian byte order)

  • f - float (machine dependent size and representation)

  • g - float (machine dependent size, little endian byte order)

  • G - float (machine dependent size, big endian byte order)

  • d - double (machine dependent size and representation)

  • e - double (machine dependent size, little endian byte order)

  • E - double (machine dependent size, big endian byte order)

  • x - NUL byte

  • X - Back up one byte

  • Z - NUL-padded string

  • @ - NUL-fill to absolute position

args+Specifies one or more arguments to be packed

PHP Notes:

  • When using PHP, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.