Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

decbin

Converts a number to binary.

Syntax

decbin(int $num): string

Parameters

ParameterTypeDescription
$numintAn integer.

Return value

stringA string of zeros and ones.

Examples

Decimal to binary with leading zeros:

<?php
 
echo decbin(10), "\n";
echo str_pad(decbin(5), 8, '0', STR_PAD_LEFT);

Output:

1010
00000101

Notes

Leading zeros are not printed — add them with str_pad().

See also

  • bindec — Converts a binary string to a decimal number.
  • dechex — Converts a number to hexadecimal.
  • base_convert — Converts a number from one base to another.

Related recipes

In the tutorial

  • Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.

Thanks for reading!

Donate to the site — we work hard for you.