bindec() reads a binary string and returns its decimal value. Keep the input limited to binary digits when accepting user-supplied values.
$binary = '101101';
echo bindec($binary); // 45
echo bindec('1101'); // Output 13
echo "<br>";
echo bindec('0101'); // Output 5
echo "<br>";
echo bindec('10110'); // Output is 22
echo "<br>";
echo bindec('101101101'); // Output is 365
echo "<br>";
echo bindec('111011100'); // Output is 476
echo "<br>";
Syntax
int|float bindec(string $binary);
| Parameter | DESCRIPTION |
|---|---|
| $binary | Required : Binary number as Input string representing number to convert to equivalent decimal number. |
0 and 1 when it comes from external input.$binary = "000101";
echo bindec($binary); // Output: 5
$binary = "110010101011011010";
echo bindec($binary); // Output: 207610
$binary = "1011";
if (preg_match('/^[01]+$/', $binary)) {
echo bindec($binary); // Output: 11
}
$binary = "1010 0011"; // Spaces between bits
$binary_clean = str_replace(' ', '', $binary); // Remove spaces
echo bindec($binary_clean); // Output: 163
$binary = "1001011";
$decimal = bindec($binary);
echo "Binary: $binary to Decimal: $decimal"; // Output: Binary: 1001011 to Decimal: 75
$binary = "101.101";
list($intPart, $fracPart) = explode('.', $binary);
$decimal = bindec($intPart) + bindec($fracPart) / pow(2, strlen($fracPart));
echo $decimal; // Output: 5.625
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.