<?Php
echo decbin(3); // Output 11
echo "<br>";
echo decbin(35); // Output 100011
echo "<br>";
echo decbin(100); // Output is 1100100
echo "<br>";
echo decbin(258); // Output is 100000010
echo "<br>";
echo decbin(1000); // Output is 1111101000
echo "<br>";
?>
Syntax
string decbin(int $number);
Parameter | DESCRIPTION |
---|---|
$number | Required : Decimal number as Input to convert to equivalent binary number. |
<?php
echo decbin(-5); // Output: '-101' (Two's complement is not handled by decbin)
?>
<?php
echo decbin(9999999); // Output: 100110001001011001111111
?>
<?php
$a = 5; // 101 in binary
$b = 3; // 11 in binary
echo decbin($a & $b); // Output: 1 (bitwise AND)
?>
<?php
$number = 7;
echo str_pad(decbin($number), 8, '0', STR_PAD_LEFT); // Output: 00000111
?>
<?php
$numbers = [2, 4, 8, 16];
foreach ($numbers as $num) {
echo "Decimal: $num => Binary: " . decbin($num) . "<br>";
}
?>
<?php
$decimal = 25;
$binary = decbin($decimal);
echo "Binary: $binary <br>";
echo "Back to Decimal: " . bindec($binary); // Output: 25
?>
<?php
$decimal1 = 5; // 101 in binary
$decimal2 = 32; // 100000 in binary
echo str_pad(decbin($decimal1), 6, '0', STR_PAD_LEFT); // Output: 000101
echo "<br>";
echo str_pad(decbin($decimal2), 6, '0', STR_PAD_LEFT); // Output: 100000
?>
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.