Content · Programming · PHP

PHP bindec() Function: Usage and Examples

Learn how PHP bindec works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Converts a binary digit string into a decimal number.

What Is bindec() in PHP?

Converts a binary digit string into a decimal number.

Its syntax is:

bindec(string $binary_string): int|float

Parameters:

  • $binary_string — Accepted as string; required argument.

Return type: int|float. Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.

Availability: Available in standard PHP.

Example 1: Basic Usage

<?php

echo bindec("1010");

Output:

10

Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.

Example 2: A Different Input

<?php

echo bindec("11111111");

Output:

255

Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.

Practical Example: Applying It in a Program

<?php

$flags = ["0001", "0100", "1000"];
foreach ($flags as $flag) { echo bindec($flag) . PHP_EOL; }

Output:

1
4
8

Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.

Important Behavior and Edge Cases

  • Key behavior: Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.
  • Environment: Available in standard PHP.

Comparison With Related Functions

FunctionPurpose
bindec()converts a binary digit string into a decimal number.
decbin()decbin() converts an integer to binary text; bindec() interprets binary text as a number.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Very long values may return a float and lose exact integer precisionVery long values may return a float and lose exact integer precision. Validate external binary input before conversion.

Key Takeaways

  • converts a binary digit string into a decimal number.
  • Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.
  • Available in standard PHP.

Summary: Very long values may return a float and lose exact integer precision. Validate external binary input before conversion.

Share this resourceSend this page to someone who may find it useful.