How to use decbin() Function In PHP

The decbin() function converts the given decimal number to the binary number. It returns a string containing a binary representation of the given decimal number argument. decbin stands for decimal to binary.

Syntax:

string decbin( $value )

Example:

PHP




<?php
 
$decNum = 42;
 
$binNum = decbin($decNum);
 
echo "Binary Number: " . $binNum;
 
?>


Output

Binary Number: 101010

Decimal to Binary Conversion in PHP

Given a Decimal Number, and the task is to convert a given Decimal Number to a Binary Number using PHP.

Examples:

Input: 10
Output: 1010

Input: 25
Output: 11001

There are different methods to convert the Decimal to a Binary number, these are:

Table of Content

  • Using decbin() Function
  • Using Conversion Algorithm
  • Using Recursion

Similar Reads

Using decbin() Function

The decbin() function converts the given decimal number to the binary number. It returns a string containing a binary representation of the given decimal number argument. decbin stands for decimal to binary....

Using Conversion Algorithm

...

Using Recursion

We will use the conversion algorithm to convert the Decimal Number to its equivalent Binary Number in PHP. We will define a custom function decimalToBinary that takes a decimal number as input and iteratively divides it by 2, collecting the remainders at each step. Concatenate these remainders in reverse order to get the Binary number. The loop continues until the decimal number becomes zero....