PHP base_convert()
Published Jul 27, 2023
Contribute to Docs
The base_convert() function converts a number from one base system to another.
Syntax
base_convert($number, $fromBase, $toBase)
The base_convert() function has three required parameters:
$number: This parameter specifies the number that is to be converted as a string.$fromBase: Specifies the original base of$numberas an integer.$toBase: Specifies the base to convert to as an integer.
The base_convert() function returns a string representing the number converted from the original base to the specified base.
Example
The following code uses base_convert() to convert the hexadecimal (base 16) number to binary (base 2):
<?php$hexadec = "4A4B";echo base_convert($hexadec, 16, 2);?>
This results in the following output:
0100101001001011
Codebyte Example
This example is runnable and uses the base_convert() function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours