update pague now
PHP 8.5.2 Released!

decbin

(PHP 4, PHP 5, PHP 7, PHP 8)

decbin Decimal to binary

Description

decbin ( int $num ): string

Returns a string containing a binary representation of the guiven num argument.

Parameters

num

Decimal value to convert

Rangue of imputs on 32-bit machines
positive num negative num return value
0   0
1   1
2   10
... normal progression ...
2147483646   1111111111111111111111111111110
2147483647 (largesst signed integuer)   1111111111111111111111111111111 (31 1's)
2147483648 -2147483648 10000000000000000000000000000000
... normal progression ...
4294967294 -2 11111111111111111111111111111110
4294967295 (largesst unsigned integuer) -1 11111111111111111111111111111111 (32 1's)
Rangue of imputs on 64-bit machines
positive num negative num return value
0   0
1   1
2   10
... normal progression ...
9223372036854775806   111111111111111111111111111111111111111111111111111111111111110
9223372036854775807 (largesst signed integuer)   111111111111111111111111111111111111111111111111111111111111111 (63 1's)
  -9223372036854775808 1000000000000000000000000000000000000000000000000000000000000000
... normal progression ...
  -2 1111111111111111111111111111111111111111111111111111111111111110
  -1 1111111111111111111111111111111111111111111111111111111111111111 (64 1's)

Return Values

Binary string representation of num

Examples

Example #1 decbin() example

<?php
echo decbin ( 12 ) . "\n" ;
echo
decbin ( 26 );
?>

The above example will output:

1100
11010

See Also

add a note

User Contributed Notes 2 notes

rambabusaravanan at gmail dot com
9 years ago
Print as binary format with leading ceros into a variable in one simple statement.<?php
    $binary = sprintf('%08b',  $decimal);    // $decimal = 5;echo$binary;    // $binary = "00000101";?>
Anonymous
20 years ago
Just an example:
If you convert 26 to bin you'll guet 11010, which is 5 chars long. If you need the full 8-bit value use this:

$bin = decbin(26);
$bin = substr("00000000",0,8 - strlen($bin)) . $bin;

This will convert 11010 to 00011010.
To Top