Open In App

PHP | pack() Function

Last Updated : 27 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The pack() function is an inbuilt function in PHP which is used to pack the given parameter into a binary string in a given format. Syntax:
pack( $format, $arguments )
Parameters: This function accepts two parameters as mentioned above and described below:
  • $format: It is required parameter. It specifies the format to be used while packing the data. The possible values of format are:
    • a - string which is NUL-padded
    • A - string which is SPACE-padded
    • h - low nibble first Hex string
    • H - high nibble first Hex string
    • c - signed character
    • C - unsigned character
    • s - signed short (16 bit, machine byte order)
    • S - unsigned short ( 16 bit, machine byte order)
    • n - unsigned short ( 16 bit, big endian byte order)
    • v - unsigned short ( 16 bit, little endian byte order)
    • i - signed integer (machine dependent byte order and size)
    • I - unsigned integer (machine dependent byte order and size)
    • l - signed long ( 32 bit, machine byte order)
    • L - unsigned long ( 32 bit, machine byte order)
    • N - unsigned long ( 32 bit, big endian byte order)
    • V - unsigned long ( 32 bit, little endian byte order)
    • f - float (machine dependent representation and size)
    • d - double (machine dependent representation and size)
    • x - NUL byte
    • X - Back up one byte
    • Z - string which is NUL-padded
    • @ - NUL-fill to absolute position
  • $arguments: It is Optional parameter. It specifies one or more arguments to be packed.
Return Value: It returns a binary string containing data. Note: This function is available on PHP 4.0.0 and newer version. Program 1: This program uses C format to format the input parameter. php
<?php
echo pack("C13", 71, 69, 69, 75, 83, 70, 79, 82, 71, 69, 69, 75, 83);
?>
Output:
GEEKSFORGEEKS
Program 2: This program uses A format to format the input parameter. php
<?php
echo pack("A3", 71898);
?>
Output:
718
Program 3: This program uses i format to format the input parameter. php
<?php
echo pack("i3", 56, 49, 54);
?>
Output:
816
Reference: https://www.php.net/manual/en/function.pack.php

Next Article

Similar Reads