PHP Program to Convert Decimal to Binary (Using Loop, Recursion, Bitwise, Built-in, and More)
Introduction
Binary numbers (composed of 0s and 1s) are the fundamental building blocks of digital systems. As a server-side scripting language, PHP can easily handle base conversions.
In this article, we will write 5+ different PHP programs to convert a decimal number to binary using loops, recursion, bitwise, and other approaches.
Problem Statement
Write multiple PHP programs to convert a decimal number into binary using various logic-based and built-in methods.
Use Cases
-
For educational tools and online compilers
-
Backend scripts handling IP/binary flags
-
Logical problem-solving for students learning PHP
-
Building number system converters in web apps
Method 1: Using While Loop with String Concatenation
<!-- Program 1: Convert Decimal to Binary using While Loop -->
<?php
function decimalToBinaryLoop($number) {
if ($number == 0) return "0";
$binary = "";
while ($number > 0) {
$binary = ($number % 2) . $binary;
$number = intdiv($number, 2);
}
return $binary;
}
echo "Binary (34): " . decimalToBinaryLoop(34);
?>
Explanation
-
Start with an empty binary string.
-
Divide the number by 2 repeatedly.
-
Store each remainder (0 or 1) from right to left.
Output
Binary (34): 100010
Method 2: Using Recursion
<!-- Program 2: Convert Decimal to Binary using Recursion -->
<?php
function decimalToBinaryRecursive($number) {
if ($number == 0) return "";
return decimalToBinaryRecursive(intdiv($number, 2)) . ($number % 2);
}
$result = decimalToBinaryRecursive(15);
echo "\nBinary (15): " . ($result === "" ? "0" : $result);
?>
Explanation
-
The function calls itself until the number is 0.
-
On return, the remainder is appended to the binary result.
-
Base case: return empty string for 0.
Output
Binary (15): 1111
Method 3: Using Bitwise Operators
<!-- Program 3: Convert Decimal to Binary using Bitwise -->
<?php
function decimalToBinaryBitwise($number) {
$binary = "";
$started = false;
for ($i = 31; $i >= 0; $i--) {
$bit = ($number >> $i) & 1;
if ($bit == 1) $started = true;
if ($started) $binary .= $bit;
}
return $binary === "" ? "0" : $binary;
}
echo "\nBinary (29): " . decimalToBinaryBitwise(29);
?>
Explanation
-
Shift number right by
i
bits. -
Use bitwise AND with 1 to extract the current bit.
-
Skip leading zeros until the first 1.
Output
Binary (29): 11101
Method 4: Using PHP Built-in decbin()
Function
<!-- Program 4: Convert Decimal to Binary using Built-in Function -->
<?php
$number = 20;
echo "\nBinary (20): " . decbin($number);
?>
Explanation
-
decbin()
is a built-in PHP function. -
It converts any decimal number directly to a binary string.
Output
Binary (20): 10100
Method 5: Stack Logic with Array
<!-- Program 5: Convert Decimal to Binary using Array Stack Simulation -->
<?php
function decimalToBinaryStack($number) {
if ($number == 0) return "0";
$stack = array();
while ($number > 0) {
array_push($stack, $number % 2);
$number = intdiv($number, 2);
}
return implode('', array_reverse($stack));
}
echo "\nBinary (39): " . decimalToBinaryStack(39);
?>
Explanation
-
Simulate stack using
array_push()
. -
Push all remainders.
-
Reverse and join the binary digits.
Output
Binary (39): 100111
Method 6: Manual Left Padding Logic
<!-- Program 6: Convert Decimal to Binary using Left Padding -->
<?php
function decimalToBinaryManual($number) {
if ($number == 0) return "0";
$binary = "";
while ($number > 0) {
$bit = $number % 2;
$binary = $bit . $binary;
$number = intdiv($number, 2);
}
return str_pad($binary, 8, "0", STR_PAD_LEFT); // Optional padding
}
echo "\nBinary (9): " . decimalToBinaryManual(9);
?>
Explanation
-
Prepend binary digits manually.
-
Optionally, pad with leading 0s to 8-bit format using
str_pad
.
Output
Binary (9): 00001001
Conclusion
You now have a collection of 6 different PHP programs to perform decimal to binary conversion using loops, recursion, bitwise logic, arrays, and PHP’s built-in tools. This approach improves logical thinking and also teaches PHP's strengths in control flow and data manipulation.