What is the decbin method In PHP?

The decbin method in PHP can be used to convert a decimal number to a binary string.

Syntax

decbin(int $num): string

Parameters

The argument is the decimal number that will be converted to binary.

Return value

decbin returns the binary value of the argument as a string.

Code

<?php
$num = 1;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 2;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 3;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 4;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 5;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 6;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 7;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
$num = 8;
echo "binary value of ". $num. " is: ". decbin($num)."\n";
?>

Explanation

In the code above, we use the decbin method to convert the decimal numbers from 1 to 8 into binary representation.

Free Resources