Signature
intval(mixed $value, int $base = 10): intPurpose
intval() returns the integer value of a variable. It is the function form of an (int) cast, with one extra ability: when the input is a string, you can tell it which numeric base to use. Typical uses are turning form or query-string text into whole numbers, truncating floats, and parsing hexadecimal, binary or octal text.
Signature
Parameters and defaults
$valueis the value to convert. It is meant for scalars, but arrays are accepted too.$baseis the base used for conversion and defaults to10. It only has an effect when$valueis a string. For an integer or float, the base is ignored, sointval(42, 8)is still42.
When $base is 0, PHP picks the base from the string's prefix:
0xor0Xmeans base 16.0bor0Bmeans base 2.- A leading
0means base 8. - Anything else is base 10.
Return value and failure cases
The function always returns an int. It does not throw for ordinary input. If nothing numeric can be read, the result is 0. This means intval('abc') and intval('0') both give 0, so you cannot use the result alone to tell invalid input from a real zero.
The usual integer-casting rules apply:
- Floats are truncated toward zero, so
4.7becomes4and-4.7becomes-4. - Strings are read from the left. Leading whitespace and a sign are accepted, and reading stops at the first character that does not fit.
'42abc'becomes42. truebecomes1, whilefalseandnullbecome0.- An empty array gives
0and a non-empty array gives1. - An object triggers an
E_WARNINGand gives1. Do not callintval()on objects.
The maximum value depends on the platform. On 64-bit systems it is 9223372036854775807. A decimal string that is too large is clamped to the maximum rather than wrapping.
Example 1: common conversions
This loop shows how several kinds of input are converted with the default base.
$values = [42, 4.7, -4.7, '42', '+42', ' 42', '42abc', 'abc', true, false, null];
$result = [];
foreach ($values as $value) {
$result[] = intval($value);
}
echo json_encode($result), PHP_EOL;[42,4,-4,42,42,42,42,0,1,0,0]
Note that '42abc' yields 42 without any warning. Prefix parsing is silent.
Example 2: working with bases
The base matters only for strings. Passing 0 lets the prefix decide.
$result = [
'hex_prefix_base10' => intval('0x1A'),
'hex_auto' => intval('0x1A', 0),
'hex_base16' => intval('1A', 16),
'binary_auto' => intval('0b11', 0),
'octal_auto' => intval('012', 0),
'octal_base8' => intval('42', 8),
'base36' => intval('1ya', 36),
'int_ignores_base' => intval(42, 8),
];
echo json_encode($result), PHP_EOL;{"hex_prefix_base10":0,"hex_auto":26,"hex_base16":26,"binary_auto":3,"octal_auto":10,"octal_base8":34,"base36":2530,"int_ignores_base":42}
With the default base 10, '0x1A' reads only the leading 0 and stops at x, so the result is 0.
Example 3: edge cases
Arrays, scientific notation and oversized strings all follow specific rules.
$result = [
'empty_array' => intval([]),
'non_empty_array' => intval(['a', 'b']),
'sci_string' => intval('1e3'),
'decimal_string' => intval('12.9'),
'big_string_is_max' => intval('420000000000000000000') === PHP_INT_MAX,
];
echo json_encode($result), PHP_EOL;{"empty_array":0,"non_empty_array":1,"sci_string":1000,"decimal_string":12,"big_string_is_max":true}
A numeric string in scientific notation such as '1e3' is parsed as a number first and then converted, giving 1000. A plain prefix read would have stopped at 1.
Edge cases to remember
- Leading zeros.
intval('042')is42because the default base is 10. The integer literal042in source code is octal and equals34. With base0, the string'042'is read as octal. - Scientific notation. Because the numeric part is parsed as a whole,
'42.42e42'is not42. A huge exponent can overflow toPHP_INT_MAX. - Out-of-range floats. Converting a float that does not fit in an integer, such as
420000000000000000000, does not clamp the way a string does. Do not rely on the value you get. - Platform size. On a 32-bit build the range is much smaller, so very large strings clamp to
2147483647.
Common mistakes
- Treating
0as an error signal.intval('hello')andintval('0')are indistinguishable. Check withis_numeric()orfilter_var()first when invalid input must be rejected. - Passing a base with a non-string. The base is ignored for ints and floats, so
intval(42, 8)does not give34. Pass the string'42'if you want octal parsing. - Expecting rounding.
intval(4.9)is4. Useround()first if you want the nearest integer. - Expecting hex parsing at base 10.
'0x1A'needs base0or base16handled correctly, and the prefix handling differs between them. - Calling it on objects. The result is a warning and the value
1, which is almost never what you meant.
Related functions
boolval()gets the boolean value of a variable.floatval()gets the float value of a variable.strval()gets the string value of a variable.settype()changes the type of a variable in place.is_numeric()tests whether a value is a number or numeric string.
Recap
intval() converts a value to int using the same rules as an (int) cast. It truncates floats, reads strings from the left, and returns 0 when nothing numeric is found. The $base argument applies only to strings, and base 0 detects the base from a 0x, 0b or 0 prefix. Validate input separately when 0 is a legitimate value, and never pass objects.
Tested examples
$values = [42, 4.7, -4.7, '42', '+42', ' 42', '42abc', 'abc', true, false, null];
$result = [];
foreach ($values as $value) {
$result[] = intval($value);
}
echo json_encode($result), PHP_EOL;
[42,4,-4,42,42,42,42,0,1,0,0]
$result = [
'hex_prefix_base10' => intval('0x1A'),
'hex_auto' => intval('0x1A', 0),
'hex_base16' => intval('1A', 16),
'binary_auto' => intval('0b11', 0),
'octal_auto' => intval('012', 0),
'octal_base8' => intval('42', 8),
'base36' => intval('1ya', 36),
'int_ignores_base' => intval(42, 8),
];
echo json_encode($result), PHP_EOL;
{"hex_prefix_base10":0,"hex_auto":26,"hex_base16":26,"binary_auto":3,"octal_auto":10,"octal_base8":34,"base36":2530,"int_ignores_base":42}
$result = [
'empty_array' => intval([]),
'non_empty_array' => intval(['a', 'b']),
'sci_string' => intval('1e3'),
'decimal_string' => intval('12.9'),
'big_string_is_max' => intval('420000000000000000000') === PHP_INT_MAX,
];
echo json_encode($result), PHP_EOL;
{"empty_array":0,"non_empty_array":1,"sci_string":1000,"decimal_string":12,"big_string_is_max":true}