PHP decbin() Function

The decbin() function converts a decimal number to its binary representation and returns it as a string. This function is useful for displaying numbers in binary format or for bitwise operations.

Syntax

decbin ( int $number ) : string

Parameters

Parameter Description
number A decimal number to be converted to binary representation

Return Value

Returns a string containing the binary representation of the given decimal number.

Examples

Basic Usage

Here's how to convert positive decimal numbers to binary ?

<?php
    echo "decbin(10) = " . decbin(10) . "<br>";
    echo "decbin(255) = " . decbin(255) . "<br>";
    echo "decbin(1024) = " . decbin(1024) . "<br>";
?>
decbin(10) = 1010
decbin(255) = 11111111
decbin(1024) = 10000000000

Negative Numbers

When converting negative numbers, decbin() returns the two's complement representation ?

<?php
    $arg = -10;
    $val = decbin($arg);
    echo "decbin(" . $arg . ") = " . $val;
?>
decbin(-10) = 1111111111111111111111111111111111111111111111111111111111110110

Practical Example

Converting multiple values and formatting the output ?

<?php
    $numbers = [8, 16, 32, 64];
    
    foreach ($numbers as $num) {
        printf("Decimal: %3d | Binary: %s<br>", $num, decbin($num));
    }
?>
Decimal:   8 | Binary: 1000
Decimal:  16 | Binary: 10000
Decimal:  32 | Binary: 100000
Decimal:  64 | Binary: 1000000

Key Points

  • Available in PHP 4.x, 5.x, 7.x, and 8.x
  • Returns a string, not an integer
  • Handles negative numbers using two's complement
  • Can be combined with other base conversion functions like bindec() for reverse conversion

Conclusion

The decbin() function is essential for converting decimal numbers to binary format. It's particularly useful in applications involving bitwise operations, binary data representation, or educational purposes where binary visualization is needed.

Updated on: 2026-03-15T08:55:52+05:30

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements