bindec

Converts a binary string to its decimal value, returning an int or a float when too large.

Signature
bindec(string $binary_string): int|float
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$binary_stringstringThe binary string to convert. Characters other than 0 and 1 are silently ignored.

Return value

int|floatThe decimal value of binary_string, as an int, or as a float when the value exceeds the int range.

Errors

Emits a deprecation notice since PHP 7.4 when binary_string contains characters other than 0 and 1. Throws a TypeError under strict_types if a non-string is passed.

Changelog

  • PHP 7.4.0Passing invalid characters now generates a deprecation notice. The result is still computed as if the invalid characters did not exist.

bindec() reads a string of ones and zeros and gives back the number it represents. Leading zeros are harmless, so '00110011' and '110011' both give 51.

When to reach for it

Use it when the binary comes to you as text: a bitmask stored as a string in a config file or database column, a permissions field rendered as '1101' in an API response, a value copied from a datasheet, or the output of decbin() being read back. For binary you are writing by hand in source code, the 0b1101 literal is a real integer already and does not need conversion.

Its partner is decbin(); together they let you inspect and rebuild bit flags in a readable form.

What trips people up

Two things. First, bindec() treats the input as unsigned — the leading bit is another order of magnitude, never a sign bit — so '11111111' is 255 and never -1. Recovering a signed value from a two's-complement string is manual work you must do yourself.

Second, a string longer than 63 bits exceeds the integer range and the return type silently becomes float, at which point bitwise operators no longer behave and precision is gone. Check with is_int() if the width can vary.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Read a permission bitmask stored as a '1011' string in a database column.
  • Rebuild an integer from a binary string produced by decbin() elsewhere.
  • Decode a fixed-width binary field described in a hardware datasheet.

Frequently asked questions

Why does bindec() return a positive number for a binary string with a leading 1?

Because bindec() interprets the string as unsigned: the most significant bit counts as magnitude, not as a sign. To interpret a two's-complement value you must subtract 2 raised to the bit width yourself when the top bit is set.

What happens if the binary string contains other characters?

They are silently ignored and the result is computed from the remaining 0s and 1s. Since PHP 7.4 this also raises a deprecation notice, so validate the string with a check such as preg_match('/^[01]+$/', $s) first.

What is the difference between bindec() and 0b notation?

0b1101 is an integer literal the parser handles at compile time, while bindec('1101') converts a runtime string. Use the literal in source code and bindec() for data.

More examples

Permission flags from a binary string

Read and write are combined into a single mask of 6. The two checks print true and false, showing the write flag is set and the execute flag is not, and decbin() prints 110 to confirm the stored bit pattern.

Example · php
<?php
$read  = bindec('100');
$write = bindec('010');
$exec  = bindec('001');
$perms = $read | $write;
var_dump(($perms & $write) !== 0);
var_dump(($perms & $exec) !== 0);
echo decbin($perms), PHP_EOL;

Where int turns into float

PHP_INT_MAX is 63 ones and converts back to an exact int. Adding one more bit pushes the value past the integer range, so the return type flips to float and is_int() reports false — the point at which bitwise operations stop being reliable.

Example · php
<?php
$maxInt = decbin(PHP_INT_MAX);
echo strlen($maxInt), " bits", PHP_EOL;
var_dump(bindec($maxInt));
var_dump(bindec('1' . $maxInt));
var_dump(is_int(bindec('1' . $maxInt)));

Discussion

  • Be the first to comment on this lesson.