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.
<?php echo pack("C13", 71, 69, 69, 75, 83, 70, 79, 82, 71, 69, 69, 75, 83); ?> |
GEEKSFORGEEKS
Program 2: This program uses A format to format the input parameter.
<?php echo pack("A3", 71898); ?> |
718
Program 3: This program uses i format to format the input parameter.
<?php echo pack("i3", 56, 49, 54); ?> |
816
Reference: https://www.php.net/manual/en/function.pack.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Set add() Function
- PHP | pi( ) Function
- PHP | Ds\Map xor() Function
- PHP | pow( ) Function
- PHP | each() Function
- PHP | Ds\Map put() Function
- D3.js | d3.max() function
- PHP | Ds\Set xor() Function
- CSS | rgb() Function
- D3.js | d3.min() function
- PHP | min( ) Function
- PHP | key() Function
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




