base_convert()
base_convert() — Convert a number between arbitrary bases.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns a string containing num represented in base to_base. The base in which num is given is specified in from_base. Both from_base and to_base have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35. The case of the letters doesn't matter, i.e. num is interpreted case-insensitively.
Signature
base_convert(string $num, int $from_base, int $to_base): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $num | string | The number to convert. Any invalid characters in num are silently ignored. As of PHP 7.4.0 supplying any invalid characters is deprecated. |
| $from_base | int | The base num is in |
| $to_base | int | The base to convert num to |
Return value
num converted to base to_base
Example
A runnable snippet:
$hexadecimal = 'a37334'; echo base_convert($hexadecimal, 16, 2);
Output:
101000110111001100110100
Changelog
| Version | Change |
|---|---|
| 8.0.0 | Now throws a ValueError if from_base or to_base is out of range. Previously, an E_WARNING was raised and false was returned. |
| 7.4.0 | Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page base_convert() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.