PHP | unpack() Function Last Updated : 09 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The unpack() function is an inbuilt function in PHP which is used to unpack from a binary string into the respective format. Syntax: array unpack( $format, $data, $offset ) Parameters: This function accepts three parameters as mentioned above and described below: $format: It is required parameter. It specifies the format to be used while packing data. a - denotes string which is NUL-padded. A - denotes string which is SPACE-padded. h - denotes low nibble first Hex string. H - denotes high nibble first Hex string. c - denotes signed character. C - denotes unsigned character. s - denotes signed short (16 bit, machine byte order). S - denotes unsigned short (16 bit, machine byte order). n - denotes unsigned short (16 bit, big endian byte order). v - denotes unsigned short (16 bit, little endian byte order). i - denotes signed integer (machine dependent byte order and size). I - denotes unsigned integer (machine dependent byte order and size). l - denotes signed long (32 bit, machine byte order). L - denotes unsigned long (32 bit, machine byte order). N - denotes unsigned long (32 bit, big endian byte order). V - denotes unsigned long (32 bit, little endian byte order). f - denotes float (machine dependent representation and size). d - denotes double (machine dependent representation and size). x - denotes NUL byte. X - denotes Back up one byte. Z - denotes string which is NUL-padded. @ - denotes NUL-fill to absolute position. $data: It is Required parameter. It specifies the binary data to be unpacked. offset: This parameter holds the offset to begin from unpacking. Return Value: It returns an associative array containing unpacked elements on success, or returns FALSE on failure. Note: This function is available for PHP 4.0.0 and newer version. Example 1: This program uses C format to unpack the data from binary string. php <?php var_dump( unpack("C*", "GEEKSFORGEEKS")); ?> Output: array(13) { [1]=> int(71) [2]=> int(69) [3]=> int(69) [4]=> int(75) [5]=> int(83) [6]=> int(70) [7]=> int(79) [8]=> int(82) [9]=> int(71) [10]=> int(69) [11]=> int(69) [12]=> int(75) [13]=> int(83) } Example 2: php <?php $binary_data = pack("c2n2", 0x1634, 0x3623, 65, 66); var_dump(unpack("c2chars/n2int", $binary_data)); ?> Output: array(4) { ["chars1"]=> int(52) ["chars2"]=> int(35) ["int1"]=> int(65) ["int2"]=> int(66) } Example 3: This example uses i format to unpack the data from binary string. php <?php $binary_data = pack("i3", 56, 49, 54); var_dump(unpack("i3", $binary_data)); ?> Output: array(3) { [1]=> int(56) [2]=> int(49) [3]=> int(54) } Reference: https://www.php.net/manual/en/function.unpack.php Comment More infoAdvertise with us Next Article PHP | urldecode() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | unlink() Function The unlink() function in PHP is used to delete a file from the filesystem. It does not delete directories. For that, you would need to use rmdir() or other methods.Syntax:unlink( $filename, $context )In this syntax:$filename: It is a mandatory parameter that specifies the filename of the file that h 3 min read PHP | unset() Function In PHP, the unset() function is used to destroy a variable, array element, or object. Once a variable or array element is "unset", it is completely removed from memory, and it no longer exists in the program. This function is essential for freeing up memory by removing unwanted or unnecessary data d 3 min read PHP | rewind( ) Function The rewind() function in PHP is an inbuilt function which is used to set the position of the file pointer to the beginning of the file. Any data written to a file will always be appended if the file is opened in append ("a" or "a+") mode regardless of the file pointer position. The file on which the 2 min read PHP | urldecode() Function The urldecode() function is an inbuilt function in PHP which is used to decode url that is encoded by the encode () function. Syntax:string urldecode( $input )Parameters: This function accepts a single parameter $input which holds the url to be decoded. Return Value: This function returns the decode 1 min read 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 speci 2 min read PHP String Functions Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling s 6 min read Like