PHP bindec() Function
The PHP bindec() function returns the decimal number equivalent to the binary number represented by the binary string argument.
The table below shows binary numbers converted into decimal numbers.
Binary | Decimal | Binary | Decimal |
---|---|---|---|
0 | 0 | 1111 | 15 |
1 | 1 | 10000 | 16 |
10 | 2 | 11111 | 31 |
11 | 3 | 100000 | 32 |
100 | 4 | 111111 | 63 |
101 | 5 | 1000000 | 64 |
110 | 6 | 1111111 | 127 |
111 | 7 | 10000000 | 128 |
1000 | 8 | 111110100 | 500 |
Syntax
bindec(bin_string)
Parameters
bin_string |
Required. Specify the binary number to convert. |
Note: The parameter must be a string. Using other data types will produce unexpected results.
Return Value
The decimal representation of a binary string argument.
Example:
In the example below, bindec() function returns the decimal representation of a binary string argument.
<?php echo "bindec(0) = ".bindec(0)."\n"; echo "bindec(1) = ".bindec(1)."\n"; echo "bindec(10) = ".bindec(10)."\n"; echo "bindec(11) = ".bindec(11)."\n"; echo "bindec(100) = ".bindec(100)."\n"; echo "bindec(111) = ".bindec(111)."\n"; echo "bindec('11111') = ".bindec('11111')."\n"; echo "bindec('111011') = ".bindec('111011')."\n"; ?>
The output of the above code will be:
bindec(0) = 0 bindec(1) = 1 bindec(10) = 2 bindec(11) = 3 bindec(100) = 4 bindec(111) = 7 bindec('11111') = 31 bindec('111011') = 59
❮ PHP Math Reference