W3docs

base_convert()

Today, we will discuss the base_convert() function in PHP. This function is used to convert a number from one base to another.

The base_convert() function converts a number from one numeral base to another — for example, from decimal (base 10) to binary (base 2), or from hexadecimal (base 16) back to decimal. It is the most general of PHP's base-conversion helpers, because it works with any base from 2 to 36, whereas functions like dechex() or bindec() are locked to a single pair of bases.

This page covers the syntax, the valid range of bases, a set of runnable examples, the gotchas (case sensitivity, fractions, precision limits), and when to reach for a more specific function instead.

Syntax

base_convert(string $num, int $from_base, int $to_base): string
ParameterDescription
$numThe number to convert, given as a string (an integer is also accepted and cast to string).
$from_baseThe base $num is currently written in. Must be between 2 and 36.
$to_baseThe base to convert $num to. Must be between 2 and 36.

Return value: the converted number as a string, always in lowercase. Bases above 10 use the letters az for digits 10–35 (so base 16 uses 0-9 and a-f, base 36 uses 0-9 and a-z).

A basic example

The most common use is converting a decimal number to binary:

php— editable, runs on the server

Decimal 10 becomes the string "1010" in binary. Note the result is a string, not an integer — keep that in mind if you plan to do arithmetic on it afterwards.

Converting between common bases

The same function handles hexadecimal, octal, and any base in between. Here are several conversions side by side:

<?php
echo base_convert("ff", 16, 10), "\n";   // hex -> decimal: 255
echo base_convert("255", 10, 16), "\n";  // decimal -> hex: ff
echo base_convert("777", 8, 10), "\n";   // octal -> decimal: 511
echo base_convert("a37334", 16, 2), "\n";// hex -> binary
echo base_convert("zz", 36, 10), "\n";   // base 36 -> decimal: 1295
?>

This prints:

255
ff
511
101000110111001100110100
1295

Because base_convert() understands both ends of the conversion, you do not need to chain two separate calls to, say, go from hexadecimal to binary.

Things to watch out for

  • Case is normalized. Input letters may be uppercase or lowercase ("FF" and "ff" both work), but the output is always lowercase.
  • Fractions are not supported. base_convert() works on whole numbers only. A value like "12.34" has its . treated as an invalid character — it is stripped and only the digits are converted (and PHP 8.1+ emits a deprecation notice for the ignored characters).
  • Invalid digits are ignored. A character that is not valid in $from_base (for example a 9 when $from_base is 8) is silently dropped rather than raising an error, so validate your input first.
  • Precision is limited. Internally the value is handled as a floating-point number. Very large integers can lose precision and produce an inexact result. For arbitrary-precision base conversion, use the GMP extension's gmp_init() with a base instead.

When to use a more specific function

If you are always converting to or from base 10, the dedicated functions are clearer and slightly faster:

Reach for base_convert() when neither base is 10 (e.g. hex → binary) or when the base is something unusual like base 36 (often used for short, URL-friendly IDs).

Conclusion

base_convert() is PHP's general-purpose tool for moving a whole number between any two numeral systems from base 2 to base 36. Remember that it returns a lowercase string, ignores fractions and invalid digits, and is limited by floating-point precision for very large values. For the common decimal-to-X conversions, the dedicated dec*() / *dec() helpers are usually the better choice.

Practice

Practice
What does the base_convert() function in PHP do?
What does the base_convert() function in PHP do?
Was this page helpful?