PHP bindec() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP bindec() function converts a binary number, written as a string of 0s and 1s, into a decimal number.

It is useful when you read binary data or flags and want to see them as normal numbers.

Syntax

Call bindec() with the values you need. Start with this line.

PHP
$decimal = bindec($binary_string);
CallWhat it does
bindec('1101')Returns 13.
bindec('11111111')Returns 255.
bindec('0')Returns 0.

Pass the binary number as a string. A plain integer like 1101 also works, but a string is safer for long values with leading zeros.

Library declarations
PHP
bindec(string $binary_string): int|float

Parameters

ParameterDescription
binary_stringRequired. The binary number to convert, made only of the characters 0 and 1.

Return Value

Return ValueDescription
int|floatThe decimal value of the binary string. It returns a float if the number is bigger than PHP_INT_MAX.

PHP Examples

Example 1 — Basic bindec() usage

This example converts a few binary strings into decimal numbers.

PHP
<?php
echo bindec('1101') . PHP_EOL;
echo bindec('11111111') . PHP_EOL;
echo bindec('110011') . PHP_EOL;
echo bindec('1') . PHP_EOL;

How It Works

  1. 1101 means 8 + 4 + 0 + 1, which is 13.
  2. Eight 1 digits give 255, the biggest value of one byte.
  3. 110011 means 32 + 16 + 0 + 0 + 2 + 1, which is 51.
  4. A single 1 is simply 1.

Example 2 — Leading zeros and very long strings

Leading zeros do not change the value. Very long strings can grow past the size of an integer, so bindec() returns a float.

PHP
<?php
var_dump(bindec('00001010'));
var_dump(bindec('0'));
var_dump(bindec(str_repeat('1', 63)));
var_dump(bindec(str_repeat('1', 64)));

How It Works

  1. '00001010' is the same as 1010, so the result is int(10).
  2. A single zero gives int(0).
  3. 63 ones fit exactly into PHP_INT_MAX, so the result is still an int.
  4. 64 ones are too big for an int, so PHP returns a float.

Example 3 — Read binary values in a loop

Here we convert several binary strings in a loop and also test a round trip with decbin().

PHP
<?php
$values = ['0001', '0010', '1111'];

foreach ($values as $bits) {
    echo $bits . ' = ' . bindec($bits) . PHP_EOL;
}

echo bindec(decbin(37)) . PHP_EOL;

How It Works

  1. We keep three binary strings in the $values array.
  2. The foreach loop passes each string to bindec() and prints the decimal value.
  3. decbin(37) turns 37 into the string 100101.
  4. bindec() turns it back into 37, which shows the two functions are opposites.

Frequently Asked Questions

bindec() converts a binary string such as '1101' into its decimal value, which is 13 in this case.
It returns an int when the value fits in PHP_INT_MAX. For larger values it returns a float.
PHP ignores characters other than 0 and 1 and, since PHP 7.4, raises a deprecation notice.
Use decbin(). For example, decbin(13) returns '1101'.
No. It only reads 0s and 1s and always treats them as a positive number. A leading minus sign would be ignored.

Next: ceil()

ceil() function rounds a number up to the next whole number.

ceil() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful