decbin
Converts a number to binary.
Syntax
decbin(int $num): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $num | int | An 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
00000101Notes
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.