intval()
The intval() function is a built-in function in PHP that converts a variable to an integer. It is similar to the floatval() function, which converts a variable
Introduction
The intval() function returns the integer value of a variable. It is one of PHP's type-conversion functions, alongside floatval(), strval(), and boolval(). You reach for intval() whenever you have data that looks like a number but is stored as a string, a float, or some other type — for example a value pulled from a form, a URL query parameter, or a CSV file — and you need a real integer to count, index, or do arithmetic with.
This page covers the function's signature, how the optional $base parameter works, what happens with each input type, and the gotchas that trip people up.
Syntax
intval(mixed $value, int $base = 10): intIt takes up to two parameters and always returns an int:
$value— the value to convert. It can be a string, float, boolean,null, or array.$base(optional) — the numeric base to interpret$valuein. It is only used when$valueis a string; it is ignored for any other type. The default is10.
intval() never throws and never returns null. If it cannot find a number to parse, it returns 0.
Basic example
Here is the most common case — turning numeric-looking strings into integers, including non-decimal bases:
The second argument tells intval() how to read the string. "101010" in base 2 is decimal 42, and "2c" in base 16 is decimal 44. A string with no leading numeric part, like "not a number", produces 0.
Converting non-string types
When you pass something other than a string, intval() follows PHP's normal integer-casting rules and ignores $base:
<?php
echo intval(4.7) . "\n"; // 4 (floats are truncated, not rounded)
echo intval(true) . "\n"; // 1 (false → 0)
echo intval(null) . "\n"; // 0
echo intval([]) . "\n"; // 0 (empty array)
echo intval([0]) . "\n"; // 1 (non-empty array)
?>Two things to remember:
- Floats are truncated toward zero, not rounded.
intval(4.7)is4, andintval(-4.7)is-4. If you want rounding, useround()first. - Arrays convert to
0when empty and1when non-empty — almost never what you want, so don't rely on it.
How strings are parsed
For strings, intval() reads the leading numeric part and stops at the first character it can't interpret:
<?php
echo intval("12abc") . "\n"; // 12 (stops at "a")
echo intval(" 123 ") . "\n"; // 123 (leading whitespace is skipped)
echo intval("abc12") . "\n"; // 0 (starts with a non-number)
?>Auto-detecting the base with $base = 0
If you set $base to 0, intval() inspects the string's prefix and picks the base automatically:
<?php
echo intval("0x1A", 0) . "\n"; // 26 (0x → hexadecimal)
echo intval("0b101", 0) . "\n"; // 5 (0b → binary)
echo intval("042", 0) . "\n"; // 34 (leading 0 → octal)
echo intval("42", 0) . "\n"; // 42 (no prefix → decimal)
?>This is handy when parsing configuration values that may be written in different notations.
Common gotchas
Octal literals vs. octal strings. A leading zero in source code makes a number an octal literal before intval() ever runs, so intval(042) is 34 (because 042 is already 34). But the string "042" is decimal 42 under the default base 10. Keep numbers as strings if you want predictable parsing.
$base does nothing for non-strings. intval(255, 16) is 255, not 597 — the base is ignored because 255 is already an integer. Convert from a string if you mean to reinterpret the digits.
Out-of-range values. On a 64-bit platform, a value larger than PHP_INT_MAX saturates at PHP_INT_MAX rather than overflowing to a negative number.
When to use intval() vs. alternatives
- Use
intval()when you want a forgiving conversion that yields0on failure and can read other bases. - Use
(int)casting ((int)$value) for a quick decimal-only cast; it behaves likeintval()with base 10. - Use
filter_var($value, FILTER_VALIDATE_INT)when you need validation — it returnsfalsefor non-integer input instead of silently giving0, so you can tell a real0apart from a parse failure.
Conclusion
intval() converts strings, floats, booleans, and other values to an integer, with an optional base for parsing strings in binary, octal, hex, or any radix from 2 to 36. Its forgiving behavior — returning 0 on failure and truncating floats — makes it convenient, but that same forgiveness can hide bad input, so prefer filter_var() when you actually need to validate. For the related conversions, see floatval(), strval(), and gettype().