bindec()
Today, we will discuss the bindec() function in PHP. This function is used to convert a binary number to a decimal number.
The bindec() function converts a string of binary digits (base 2) into its decimal (base 10) value. It is the reverse of decbin(), and it is the function you reach for whenever you read binary data — a permission mask, a bit flag, a value parsed from text — and need an ordinary integer to work with.
Syntax
bindec(string $binary_string): int|float$binary_string— the binary number to convert, passed as a string of0s and1s (for example"1010").- Return value — the decimal equivalent. It is an
intwhen the value fits in PHP's integer range, and afloatfor very large binary numbers that would otherwise overflow.
A basic example
The binary string "1010" is 1×8 + 0×4 + 1×2 + 0×1 = 10, so bindec() returns 10.
Why pass a string and not a number?
A common first mistake is to write bindec(1010) with no quotes. That works by accident for small values, but a literal like 0101 is interpreted by PHP as an octal number long before bindec() ever sees it, and large binary literals lose leading zeros. Always pass the binary value as a quoted string so the digits arrive intact:
<?php
echo bindec("00001111"); // 15 — leading zeros are fine in a string
?>How invalid characters are handled
bindec() only understands 0 and 1. Any other character is silently dropped (this raises a deprecation notice on PHP 8.1+), so the result is not what you might expect:
<?php
echo bindec("1012"); // 5 — the "2" is ignored, leaving "101" = 5
echo "\n";
echo bindec(""); // 0 — an empty string is treated as zero
?>Because the function fails quietly, validate user-supplied input first with preg_match('/^[01]+$/', $value) before converting.
Round-tripping with decbin()
bindec() and decbin() are inverses, which is handy for inspecting or building bit masks:
<?php
$decimal = 42;
$binary = decbin($decimal); // "101010"
$back = bindec($binary); // 42
echo "$decimal -> $binary -> $back"; // 42 -> 101010 -> 42
?>A practical use: reading bit flags
Binary strings are an easy way to describe a set of on/off options. Converting to decimal lets you test individual bits with the bitwise AND operator:
<?php
define('READ', bindec("100")); // 4
define('WRITE', bindec("010")); // 2
define('EXECUTE', bindec("001")); // 1
$permissions = bindec("110"); // READ + WRITE = 6
echo ($permissions & WRITE) ? "can write\n" : "cannot write\n"; // can write
echo ($permissions & EXECUTE) ? "can execute\n" : "cannot execute\n"; // cannot execute
?>Related conversion functions
| Function | Converts |
|---|---|
decbin() | decimal → binary string |
hexdec() | hexadecimal → decimal |
octdec() | octal → decimal |
base_convert() | between any two bases (2–36) |
Conclusion
bindec() turns a binary string into a decimal number, returning an int (or a float for very large values). Remember to pass the value as a quoted string, validate it first because non-binary characters are silently ignored, and pair it with decbin() when you need to convert in the other direction. With these in hand you can comfortably work with bit masks, flags, and any data stored in base 2.