What is the hexdec method in PHP?

The hexdec method can be used to convert a hexadecimal string to a decimal number.

Syntax

hexdec(string $hex_string): int|float

Any invalid characters in the hex_string argument are skipped.

From PHP 7.4.0, any invalid character in hex_string is deprecated.

Return value

It returns the decimal number of type int or float based on the size of the value.

Code

<?php
$num = "a";
echo "HexaDecimal value of ". $num. " is: ". hexdec($num)."\n";
$num = "b";
echo "HexaDecimal value of ". $num. " is: ". hexdec($num)."\n";
$num = "c";
echo "HexaDecimal value of ". $num. " is: ". hexdec($num)."\n";
$num = "f";
echo "HexaDecimal value of ". $num. " is: ". hexdec($num)."\n";
?>

Explanation

In the code above, we used the hexdec method to convert the hexadecimal values of a, b, c and f into decimal numbers.

Free Resources