Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
bindec() function in PHP
The bindec() function converts a binary number to its decimal equivalent. It takes a binary string as input and returns the corresponding decimal value.
Syntax
bindec(binary_string)
Parameters
binary_string − The binary string to convert. Must contain only 0s and 1s.
Return Value
The bindec() function returns the decimal value of the specified binary string. If the input contains invalid characters, it stops processing at the first invalid character.
Examples
Basic Conversion
Here's a simple example converting binary to decimal ?
<?php
echo bindec("1101");
?>
13
Multiple Conversions
Converting different binary numbers ?
<?php
echo "Binary 1010 = " . bindec("1010") . "<br>";
echo "Binary 11111111 = " . bindec("11111111") . "<br>";
echo "Binary 1 = " . bindec("1") . "<br>";
echo "Binary 0 = " . bindec("0");
?>
Binary 1010 = 10 Binary 11111111 = 255 Binary 1 = 1 Binary 0 = 0
Invalid Input Handling
When invalid characters are encountered, processing stops at that point ?
<?php
echo bindec("1012") . "<br>"; // Stops at '2'
echo bindec("101abc") . "<br>"; // Stops at 'a'
echo bindec("xyz"); // No valid binary digits
?>
5 5 0
Conclusion
The bindec() function is useful for converting binary representations to decimal numbers. It processes valid binary digits until it encounters an invalid character or reaches the end of the string.
