decbin
Converts a decimal integer to its binary string representation.
decbin(int $num): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | int | Decimal value to convert. |
Return value
stringA string containing the base-2 representation of $num, without a 0b prefix and without leading zeros.
Errors
Throws a TypeError if $num is not an int or a value that can be coerced to int, such as a non-numeric string.
Changelog
- PHP 8.1Passing a float whose fractional part would be lost by the implicit int conversion emits a deprecation notice; cast explicitly with (int).
decbin() turns a decimal integer into its base-2 string form. There is no 0b prefix and no padding — decbin(5) is "101", three characters long. It is the exact inverse of bindec().
When to reach for it
Almost always for display rather than computation: dumping a flag bitmask while debugging, printing a permission set, showing what a packed integer actually contains, or teaching bitwise operators. To read or set a bit you should use &, | and << directly — converting to a string first is slower and hides the intent.
What trips people up
The width of the output varies with the value, so aligned columns need padding. Reach for sprintf('%08b', $n), or str_pad(decbin($n), 8, '0', STR_PAD_LEFT) if you already have the string.
Negative numbers are rendered as two's complement across the platform's full integer width, so decbin(-1) is 64 ones on a 64-bit build and 32 ones on a 32-bit build. You will never see a minus sign.
Values larger than PHP_INT_MAX cannot be represented at all — a float argument is truncated to int first, which since PHP 8.1 triggers a deprecation notice when precision would be lost. For arbitrary-precision conversion, use the GMP extension.
Example
When to use it
- Print a permission or feature-flag bitmask in a readable form while debugging.
- Show the bit layout of a packed integer in a log line or admin screen.
- Verify by eye that a bitwise operation set the bits you expected.
Frequently asked questions
How do I add leading zeros to decbin output?
Use sprintf('%08b', $num) to format directly to a fixed width, or str_pad(decbin($num), 8, '0', STR_PAD_LEFT) if you already have the string. decbin() itself never pads.
Why does decbin(-1) return a long string of ones?
Negative integers are converted using two's complement over the platform's full integer width, so -1 becomes 64 ones on a 64-bit build. decbin() never produces a minus sign.
What is the difference between decbin and base_convert?
decbin() only converts base 10 to base 2 and works on true integers. base_convert() handles any base from 2 to 36 but operates on strings and loses precision above PHP_INT_MAX.
More examples
Inspecting a feature-flag bitmask
The mask prints as 1011: bits for ADMIN, WRITE and READ are set, DELETE is not. Adding DELETE gives 1111 and clearing WRITE gives 1001, so you can see each bitwise operation land on exactly one column.
<?php
const READ = 1, WRITE = 2, DELETE = 4, ADMIN = 8;
$perms = READ | WRITE | ADMIN;
printf("perms = %04b (%d)\n", $perms, $perms);
printf("after +DELETE = %04b\n", $perms | DELETE);
printf("after -WRITE = %04b\n", $perms & ~WRITE);
var_dump(($perms & DELETE) !== 0);Round-tripping through bindec
Every value round-trips back to an identical int, including PHP_INT_MAX at 63 ones. Note that bindec() returns a float once the bit string exceeds the int range, which is why the identity check uses === rather than ==.
<?php
foreach ([0, 1, 42, 255, PHP_INT_MAX] as $n) {
$bits = decbin($n);
printf("%-20d -> %-64s -> %s\n", $n, $bits, var_export(bindec($bits) === $n, true));
}Counting set bits two ways
Both methods report 5 set bits. The string version reads clearly and is fine for logging, but the bitwise loop avoids allocating a string and is what you want inside a hot loop.
<?php
$n = 0b1011_0110;
echo "bits: ", decbin($n), "\n";
echo "via string: ", substr_count(decbin($n), '1'), "\n";
$count = 0;
for ($i = $n; $i !== 0; $i >>= 1) { $count += $i & 1; }
echo "via bitwise: $count\n";
Discussion