PHP base_convert() Function

Definition and Usage

The base_convert() function converts numbers between any base.

Syntax

base_convert(number,frombase,tobase)
Parameters Description
number Required. The original value.
frombase Required. The original base of the number.
tobase Required. The base to convert to.

Description

returns a string containing number with tobase representation of the base.number The base of itself is frombase specified.frombase and tobase They can only be between 2 and 36 (including 2 and 36). Numbers higher than decimal are represented by letters a-z, for example, a represents 10, b represents 11, and z represents 35.

Example

Example 1

Convert octal number to decimal number:

<?php
$oct = "0031";
$dec = base_convert($oct,8,10);
echo "The octal $oct is equal to the decimal $dec.";
?>

Output:

The octal 0031 is equal to the decimal 25.

Example 2

Convert octal number to hexadecimal number:

<?php
$oct = "364";
$hex = base_convert($oct,8,16);
echo "The octal $oct is equal to the hexadecimal $hex.";
?>

Output:

The octal 364 is equal to the hexadecimal f4.