PHP Function Reference

PHP intval() Function



The PHP intval() function returns the integer value of a variable, using the specified base for the conversion (the default is base 10).

Syntax

intval(variable, base)

Parameters

variable Required. Specify the variable to check. Must be a scalar type.
base Optional. Specify the base for the conversion. If base is not specified or 0, the base used is determined by the format of the variable:
  • if variable includes a "0x" (or "0X") prefix, the base is taken as 16 (hex)
  • if variable starts with "0", the base is taken as 8 (octal)
  • the base is taken as 10 (decimal).

Return Value

Returns the integer value of value on success, or 0 on failure. Empty arrays return 0, non-empty arrays return 1.

The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

Strings will most likely return 0 although this depends on the leftmost characters of the string. If the string is numeric or leading numeric then it will resolve to the corresponding integer value, otherwise it is converted to 0.

Note: If an object is passed to this function, it throws an E_NOTICE level error and returns 1.

Example:

The example below shows the usage of intval() function.

<?php
echo 'intval(10):        '.intval(10)."\n";
echo 'intval(10.5):      '.intval(10.5)."\n";
echo 'intval("10"):      '.intval("10")."\n";
echo 'intval("+10"):     '.intval("+10")."\n";
echo 'intval("-10"):     '.intval("-10")."\n";
echo 'intval(042):       '.intval(042)."\n";
echo 'intval("042"):     '.intval("042")."\n";
echo 'intval(1e10):      '.intval(1e10)."\n";
echo 'intval("1e10"):    '.intval("1e10")."\n";
echo 'intval(0x1A):      '.intval(0x1A)."\n";
echo 'intval("0x1A"):    '.intval("0x1A")."\n";
echo 'intval(true):      '.intval(true)."\n";
echo 'intval(false):     '.intval(false)."\n";
echo 'intval(42, 8):     '.intval(42, 8)."\n";
echo 'intval("42", 8):   '.intval("42", 8)."\n";
echo 'intval("Hello10"): '.intval("Hello10")."\n";
echo 'intval("10Hello"): '.intval("10Hello")."\n";
echo 'intval("10.5Hi"):  '.intval("10.5Hi")."\n";
?>

The output of the above code will be:

intval(10):        10
intval(10.5):      10
intval("10"):      10
intval("+10"):     10
intval("-10"):     -10
intval(042):       34
intval("042"):     42
intval(1e10):      10000000000
intval("1e10"):    10000000000
intval(0x1A):      26
intval("0x1A"):    0
intval(true):      1
intval(false):     0
intval(42, 8):     42
intval("42", 8):   34
intval("Hello10"): 0
intval("10Hello"): 10
intval("10.5Hi"):  10

Example:

Consider one more example which demonstrates the use of this function with arrays and large numbers.

<?php
$arr1 = array();
echo intval($arr1)."\n";

$arr2 = array(1, 2, 3);
echo intval($arr2)."\n";

echo intval(420000000000000000000)."\n";
echo intval('420000000000000000000')."\n";
?>

The output of the above code will be:

0
1
-4275113695319687168
9223372036854775807

❮ PHP Variable Handling Reference