PHP | pack() Function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
echo pack("C13", 71, 69, 69, 75, 83, 70, 79, 82, 71, 69, 69, 75, 83);
?>

chevron_right


Output:

GEEKSFORGEEKS

Program 2: This program uses A format to format the input parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
echo pack("A3", 71898);
?>

chevron_right


Output:

718

Program 3: This program uses i format to format the input parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
echo pack("i3", 56, 49, 54);
?>

chevron_right


Output:

816

Reference: https://www.php.net/manual/en/function.pack.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.