PHP decbin function – Decimal to binary

The PHP function decbin() converts a decimal number into a string containing its binary representation. The largest decimal value that can be converted is 2147483647. If you have to deal with larger numbers, you need to write a custom function to handle the values. It takes just one parameter, which is the number to convert. For example:

print decbin(16); // "10000"

Syntax

decbin(int $num): string

Parameters

Parameter Description
num Decimal value to convert

Examples

Example 1: Get the binary representation of an integer.

$number = 42; 
echo "The binary of $number is ", decbin($number); 

Example 2:

echo decbin(12) . "\n";
echo decbin(26);

Output:

1100
11010
Related Post