PHP decbin() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP decbin() function converts a decimal (normal) whole number into a binary string made of 0s and 1s.

It is the opposite of bindec() and is handy for looking at bits, flags and permissions.

Syntax

Call decbin() with the values you need. Start with this line.

PHP
$binary = decbin($num);
CallWhat it does
decbin(10)Returns '1010'.
decbin(255)Returns '11111111'.
decbin(0)Returns '0'.

The result is a string. Use str_pad() if you want a fixed length like 8 bits.

Library declarations
PHP
decbin(int $num): string

Parameters

ParameterDescription
numRequired. The whole number to convert to binary.

Return Value

Return ValueDescription
stringA string containing the binary form of $num, without a 0b prefix and without leading zeros.

PHP Examples

Example 1 — Basic decbin() usage

This example turns a few decimal numbers into binary strings.

PHP
<?php
echo decbin(10) . PHP_EOL;
echo decbin(255) . PHP_EOL;
echo decbin(5) . PHP_EOL;
echo decbin(0) . PHP_EOL;

How It Works

  1. 10 is 8 + 2, so the binary form is 1010.
  2. 255 needs eight 1 digits: 11111111.
  3. 5 is 4 + 1, which is 101.
  4. 0 is simply 0.

Example 2 — Negative numbers and the largest int

Negative integers use all 64 bits, so the string becomes very long. This example only prints the length of the result.

PHP
<?php
echo strlen(decbin(-1)) . PHP_EOL;
echo strlen(decbin(PHP_INT_MAX)) . PHP_EOL;
echo substr(decbin(-1), 0, 8) . PHP_EOL;

How It Works

  1. -1 is stored with all 64 bits set, so decbin(-1) has 64 characters.
  2. PHP_INT_MAX uses 63 bits, because the first bit is the sign bit.
  3. substr() takes only the first 8 characters of the negative result.
  4. Those first 8 characters are all 1, which shows the sign extension.

Example 3 — Show numbers as 8-bit values

Binary values are often shown with 8 digits. We use str_pad() to add the missing zeros on the left.

PHP
<?php
$numbers = [1, 2, 5, 37];

foreach ($numbers as $n) {
    $bits = str_pad(decbin($n), 8, '0', STR_PAD_LEFT);
    echo "$n in binary is $bits" . PHP_EOL;
}

How It Works

  1. The $numbers array holds four small whole numbers.
  2. decbin($n) converts each number to a short binary string such as 101.
  3. str_pad() adds zeros on the left until the string is 8 characters long.
  4. The loop prints each number with its 8-bit binary form.

Frequently Asked Questions

decbin() converts a decimal integer into a binary string. For example, decbin(10) returns '1010'.
It returns a string. The digits 0 and 1 are characters, not a numeric value.
Use str_pad(), for example str_pad(decbin(5), 8, '0', STR_PAD_LEFT) gives '00000101'.
It shows the 64-bit two's complement form, so decbin(-1) is a string of 64 ones.
Use bindec(). For example, bindec('1010') returns 10.

Next: dechex()

The dechex() function turns a normal decimal number into a hexadecimal (base 16) string.

dechex() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful