In this example, i’ll show you How to Convert Binary to Decimal in PHP.
PHP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?php // PHP program to convert // binary to decimal // Function to convert // binary to decimal function binaryToDecimal($n) { $num = $n; $dec_value = 0; // Initializing base value // to 1, i.e 2^0 $base = 1; $temp = $num; while ($temp) { $last_digit = $temp % 10; $temp = $temp / 10; $dec_value += $last_digit * $base; $base = $base*2; } return $dec_value; } // Driver Code $num = 1100110; echo binaryToDecimal($num), "\n"; ?> |