W3docs

decbin()

Today, we will discuss the decbin() function in PHP. This function is used to convert a decimal number to a binary number.

The decbin() function converts a decimal (base‑10) integer into its binary (base‑2) representation and returns it as a string. It is the counterpart of bindec(), which converts the other way. This page covers the syntax, the return value, the gotchas with negative numbers, and how to format the output.

Syntax

decbin(int $num): string
  • $num — the decimal integer to convert. Floats are silently truncated to integers, and booleans are cast (true becomes 1).
  • Return value — a string of 0 and 1 characters. Note it is a string, not a number, so leading zeros are never produced and the result is safe to concatenate or pad.

Basic Example

php— editable, runs on the server

We assign a decimal value, pass it to decbin(), and print the returned string. Decimal 10 is 1010 in binary (8 + 2).

Converting Several Values

Because the function returns a plain string, you can format it inline. Here are a few common conversions:

<?php
echo decbin(0);   // 0
echo "\n";
echo decbin(1);   // 1
echo "\n";
echo decbin(255); // 11111111
echo "\n";
echo decbin(256); // 100000000
?>

255 is the largest value that fits in one byte, which is why it maps to eight 1 bits.

Padding the Output to a Fixed Width

decbin() never adds leading zeros, so decbin(5) returns "101", not "00000101". When you want a fixed bit width (for example, to display every value as a byte), wrap it in str_pad():

<?php
$value = 5;

// Pad to 8 bits with leading zeros
echo str_pad(decbin($value), 8, "0", STR_PAD_LEFT); // 00000101
?>

This works precisely because the return value is a string.

Negative Numbers

PHP has no dedicated sign bit handling here: a negative integer is interpreted as an unsigned value using two's‑complement across the platform's integer width. On a 64‑bit build, decbin(-1) returns 64 ones:

<?php
echo decbin(-1);
// 1111111111111111111111111111111111111111111111111111111111111111
?>

If you need a fixed, predictable width, mask the value first — for example decbin(-1 & 0xFF) yields 11111111 (an 8‑bit view).

Round‑Tripping with bindec()

decbin() and bindec() are inverses, so converting a number to binary and back returns the original value:

<?php
$n = 42;
$binary = decbin($n);     // "101010"
$back = bindec($binary);  // 42

echo $back; // 42
?>

When Would I Use It?

  • Inspecting or displaying bit patterns while debugging bitwise operations (&, |, ^, <<, >>).
  • Building binary string representations for teaching, logging, or simple data encoding.
  • Working with permission/flag bitmasks where seeing the individual bits is helpful.

PHP ships a family of base‑conversion helpers. Pick the one that matches your source and target base:

Conclusion

The decbin() function turns a decimal integer into a binary string. Remember three things: the result is a string (so use str_pad() for fixed widths), negative numbers are shown in two's‑complement across the full integer width, and bindec() reverses the conversion.

Practice

Practice
What is the purpose of the decbin() function in PHP?
What is the purpose of the decbin() function in PHP?
Was this page helpful?