Signature
decbin(int $num): stringPurpose
decbin() converts an integer into a string made of the characters 0 and 1. It is handy when you want to see which bits of a number are set, when you debug flag masks, or when you need to show a value in base 2 for teaching or logging. The name stands for "decimal to binary", although the argument is simply a PHP integer, not a decimal-formatted string.
Signature
Parameters and defaults
$num is the only parameter and it has no default. It is the integer you want to convert. Because the declared type is int, ordinary weak-typing rules apply: an integer-like value such as "12" is accepted in normal mode, while a string such as "abc" raises a TypeError. With declare(strict_types=1) only real integers are accepted.
Return value and failure cases
The function always returns a string. For positive numbers the result has no leading zeros and no 0b prefix, so decbin(12) is "1100". Zero gives "0". There is no false return and no warning for out-of-range values; every valid integer produces some bit string.
Negative numbers are not shown with a minus sign. Instead you get the two's complement bit pattern of the integer at the machine's native integer width. On a 64-bit build that means -1 becomes 64 ones, and -2 becomes 63 ones followed by a zero. On a 32-bit build the same values give 32 characters. Code that must behave the same everywhere should not depend on the exact length of a negative result.
Example 1: basic conversion
echo decbin(12) . "\n";
echo decbin(26) . "\n";
echo decbin(0) . "\n";1100 11010 0
Each call returns a string, so concatenating a newline is straightforward. Notice that zero is a single character rather than an empty string.
Example 2: negative numbers
$negative = decbin(-2);
echo json_encode(strlen($negative) === PHP_INT_SIZE * 8) . "\n";
echo substr($negative, -4) . "\n";
echo substr_count(decbin(-1), "1") === PHP_INT_SIZE * 8 ? "all ones" : "unexpected";
echo "\n";true 1110 all ones
The length of a negative result equals the number of bits in a native integer, which is PHP_INT_SIZE * 8. Comparing against that constant keeps the example correct on any build. The last four characters of -2 are 1110, and -1 consists only of ones.
Example 3: fixed-width output
function toByte(int $n): string {
return str_pad(decbin($n & 0xFF), 8, "0", STR_PAD_LEFT);
}
echo toByte(5) . "\n";
echo toByte(255) . "\n";
echo toByte(-1) . "\n"; echo sprintf("%08b", 5) . "\n";00000101 11111111 11111111 00000101
This helper masks the value to its lowest eight bits with & 0xFF, converts it, and left-pads with zeros. Masking also tames negative input: -1 & 0xFF is 255, so the output is eight ones. The final line shows that sprintf() with %08b achieves the same padding in one step for non-negative input.
Edge cases
- Zero returns
"0", never an empty string. - No padding is added.
decbin(5)is"101", not"00000101", so pad it yourself when you need aligned columns. - Largest integer
PHP_INT_MAXyields a string of ones one bit shorter than the native width (63 characters on a 64-bit build), because the sign bit is zero. - Smallest integer
PHP_INT_MINyields a one followed by zeros across the full native width. - Floats are not a good input. A float is not an integer type, so convert deliberately with a rounding function of your choice before calling
decbin().
Common mistakes
- Expecting a minus sign.
decbin(-5)does not return"-101". If you want a signed representation, useabs()and prepend the sign yourself. - Assuming a fixed length. Positive results grow with the value, so comparing strings as if they were aligned will give wrong answers.
- Treating the result as a number. The return value is a string. Doing arithmetic on it, or comparing it with
==against an integer such as101, mixes up binary text with decimal values. - Passing binary text in.
decbin("1010")converts the decimal number one thousand and ten, not the binary pattern. Usebindec()to go the other direction. - Relying on platform width. Negative results differ between 32-bit and 64-bit builds.
Related functions
bindec()converts a binary string back to a decimal number.decoct()anddechex()produce octal and hexadecimal strings.base_convert()converts between arbitrary bases using strings.sprintf()andprintf()with%b,%032bor%064bformat binary output with padding.
Recap
decbin(int $num): string returns the base-2 text for an integer. Positive values have no leading zeros, zero is "0", and negative values show their two's complement pattern at native integer width. Pad with str_pad() or sprintf() for fixed widths, mask with & when you need a specific bit count, and use bindec() to reverse the conversion.
Tested examples
echo decbin(12) . "\n";
echo decbin(26) . "\n";
echo decbin(0) . "\n";1100 11010 0
$negative = decbin(-2);
echo json_encode(strlen($negative) === PHP_INT_SIZE * 8) . "\n";
echo substr($negative, -4) . "\n";
echo substr_count(decbin(-1), "1") === PHP_INT_SIZE * 8 ? "all ones" : "unexpected";
echo "\n";true 1110 all ones
function toByte(int $n): string {
return str_pad(decbin($n & 0xFF), 8, "0", STR_PAD_LEFT);
}
echo toByte(5) . "\n";
echo toByte(255) . "\n";
echo toByte(-1) . "\n"; echo sprintf("%08b", 5) . "\n";00000101 11111111 11111111 00000101