PHP decbin() Function


Definition and Usage

The decbin() function returns a string that contains binary equivalent of given decimal number argument.

This function returns a string with binary digits.

Syntax

decbin ( int $number ) : string

Parameters

Sr.NoParameter & Description
1number
A decimal number to be converted in equivalent binary representation

Return Values

PHP decbin() function returns a 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 binary equivalent of 13 and returns '1101' −

<?php
   $arg=13;
   $val=decbin($arg);
   echo "decbin(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

decbin(13) = 1101

Example

 Live Demo

Following example shows that fractional part of given number is ignored. Hence 9.99 is treated as 9 which '1001' in binary system. −

<?php
   $arg=9.99;
   $val=decbin($arg);
   echo "decbin(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

decbin(9.99) = 1001

Example

 Live Demo

If string is provided as argument, result is 0 −

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

Output

This will produce following result −

decbin(Hello) = 0

Example

 Live Demo

For negative decimal number, conversion is done using 2's complement method. Following example shows result on 64 bit system

<?php
   $arg=-10;
   $val=decbin($arg);
   echo "decbin(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

decbin(-10) = 1111111111111111111111111111111111111111111111111111111111110110

Updated on: 29-Jun-2020

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements