PHP bindec() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The bindec() function returns decinmal equivalent of a binary number represented as a string argument. Binary number inside string is interpreted as unigned integer.

This function returns a decimal integer. However, it may return float for size reasons.

Syntax

bindec ( string $binary_string ) : number

Parameters

Sr.NoParameter & Description
1binary_string
A string containing binary number representation. Invalid characters (other than 1 and 0) are ignored.

Return Values

PHP bindec() function returns decimal equivalent of given binary number inside string.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example calculates decimal equivalent of '1101' and returns 13 −

<?php
   $arg='1101';
   $val=bindec($arg);
   echo "bindec('" . $arg . "') = " . $val;
?>

Output

This will produce following result −

bindec('1101') = 13

Example

 Live Demo

Following example shows that characters other than 1 or 0 are ignored. Hence '110011.11' is treated as '11001111' which 207 in decimal system. −

<?php
   $arg='110011.11';
   $val=bindec($arg);
   echo "bindec('" . $arg . "') = " . $val;
?>

Output

This will produce following result −

bindec('110011.11') = 207

Example

 Live Demo

If string contains all non-binary characters, result is 0 −

<?php
   $arg='Hello';
   $val=bindec($arg);
   echo "bindec('" . $arg . "') = " . $val;
?>

Output

This will produce following result −

bindec('Hello') = 0

Example

 Live Demo

Following example shows that bindec() function treats the binary string to contain unsigned integer

<?php
   $arg='-1111';
   $val=bindec($arg);
   echo "bindec('" . $arg . "') = " . $val;
?>

Output

This will produce following result −

bindec('-1111') = 15
raja
Published on 29-Jun-2020 12:04:02
Advertisements