convert a number from base 2 to base 10 in php

To convert a binary number to decimal in PHP, you can use the built-in bindec function. This function takes a binary string as its argument and returns the equivalent decimal value.

Here's an example:

main.php
$binary = "1010"; // binary number
$decimal = bindec($binary); // convert to decimal

echo $decimal; // output: 10
115 chars
5 lines

In this example, we convert the binary number "1010" to decimal by calling the bindec function on it. The resulting decimal value is stored in the $decimal variable and then outputted to the screen.

Note that bindec only works with binary numbers. To convert numbers from other bases, you'll need to use a different function or algorithm.

gistlibby LogSnag