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
| Function | Purpose |
|---|---|
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
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: Very long values may return a float and lose exact integer precision | Very 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.