While working with numbers, many times we need to convert the bases of number and one of the most frequent used conversion is binary to decimal conversion. PHP provides us with a built-in function bindec() for this purpose. The bindec() function in PHP is used to return the decimal equivalent of the binary number. It accepts a string argument which is the binary number we want to convert to decimal.
The parameter must be a string otherwise different data types will produce unexpected results.
Syntax:
bindec(binary_string)
Parameter: This function accepts a single parameter binary_string which represents the binary string you want to convert to decimal.
Return Value: It returns the decimal value of the binary number binary_string.
Examples:
Input : bindec('110011') Output : 51 Input : bindec('000110011') Output : 51 Input : bindec('111') Output : 7
Below programs illustrate the bindec() function in PHP:
- When ‘110011’ is passed as a parameter:
<?
php
echo bindec('110011');
?>
Output:
51
- When ‘000110011’ is passed as a parameter:
<?
php
echo bindec('000110011');
?>
Output:
51
- When ‘111’ is passed as a parameter:
<?
php
echo bindec('111');
?>
Output:
7
Reference:
http://php.net/manual/en/function.bindec.php