Types7 min readExamples run on PHP 8.5.10

intval()

Learn how PHP intval() converts values to integers, how the base argument works for strings, and which edge cases and mistakes to watch for.

Signature

intval(mixed $value, int $base = 10): int

Purpose

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

  • $value is the value to convert. It is meant for scalars, but arrays are accepted too.
  • $base is the base used for conversion and defaults to 10. It only has an effect when $value is a string. For an integer or float, the base is ignored, so intval(42, 8) is still 42.

When $base is 0, PHP picks the base from the string's prefix:

  • 0x or 0X means base 16.
  • 0b or 0B means base 2.
  • A leading 0 means 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.7 becomes 4 and -4.7 becomes -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' becomes 42.
  • true becomes 1, while false and null become 0.
  • An empty array gives 0 and a non-empty array gives 1.
  • An object triggers an E_WARNING and gives 1. Do not call intval() 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.

Example
<?php
$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;
Output
[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.

Example
<?php
$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;
Output
{"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.

Example
<?php
$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;
Output
{"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') is 42 because the default base is 10. The integer literal 042 in source code is octal and equals 34. With base 0, the string '042' is read as octal.
  • Scientific notation. Because the numeric part is parsed as a whole, '42.42e42' is not 42. A huge exponent can overflow to PHP_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

  1. Treating 0 as an error signal. intval('hello') and intval('0') are indistinguishable. Check with is_numeric() or filter_var() first when invalid input must be rejected.
  2. Passing a base with a non-string. The base is ignored for ints and floats, so intval(42, 8) does not give 34. Pass the string '42' if you want octal parsing.
  3. Expecting rounding. intval(4.9) is 4. Use round() first if you want the nearest integer.
  4. Expecting hex parsing at base 10. '0x1A' needs base 0 or base 16 handled correctly, and the prefix handling differs between them.
  5. Calling it on objects. The result is a warning and the value 1, which is almost never what you meant.
  • 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

Common conversions with the default base
<?php
$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;
Output
[42,4,-4,42,42,42,42,0,1,0,0]
Working with bases
<?php
$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;
Output
{"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}
Edge cases: arrays, scientific notation, large strings
<?php
$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;
Output
{"empty_array":0,"non_empty_array":1,"sci_string":1000,"decimal_string":12,"big_string_is_max":true}