The hex2bin() function in PHP 8.5 works exactly the same as in previous versions. It decodes a hexadecimal string back into its original binary (raw) representation. There are no specific changes or deprecations for this function in the PHP 8.5 release.
Function Signature
hex2bin(string $string): string|false
Key Points
- Purpose: It converts a hex string (like "48656c6c6f") back into readable binary data (like "Hello"). It is not for converting a hex number to a binary number (use base_convert() for that).
- Errors: It will return false and throw an E_WARNING if the input string has an odd length or contains invalid hexadecimal characters (anything other than 0-9, a-f, A-F).
Example
<?php
// The hex representation of "example hex data"
$hex = "6578616d706c65206865782064617461";// Decode it back to binary
$binary = hex2bin($hex);var_dump($binary);
// Output: string(16) "example hex data"
?>
Related function: To do the reverse (convert binary data into a hex string), use bin2hex() .