(PHP 4, PHP 5, PHP 7, PHP 8)
intval — Get the integer value of a variable
Returns the int value of value,
using the specified base for the conversion
(the default is base 10). intval() should not be used
on objects, as doing so will emit an E_WARNING level
error and return 1.
valueThe scalar value being converted to an integer
baseThe base for the conversion
Note:
If
baseis 0, the base used is determined by the format ofvalue:
- if string includes a "0x" (or "0X") prefix, the base is taken as 16 (hex); otherwise,
- if string starts with a "0b" (or "0B"), the base is taken as 2 (binary); otherwise,
- if string starts with "0", the base is taken as 8 (octal); otherwise,
- the base is taken as 10 (decimal).
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. So for example
on such a system, intval('1000000000000') will return
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. The common rules of integer casting apply.
Note: Numeric strings using scientific notation (containing the letter
eorE) are first parsed as numbers before being converted to an integer. Because the numeric part of the string is parsed as a whole, the result is not simply the leading integer part. Large exponents can additionally overflow toPHP_INT_MAX:See Numeric strings for details on how such strings are interpreted.<?php
echo intval('42.42e42'); // 9223372036854775807 on 64-bit systems
?>
| Version | Description |
|---|---|
| 8.0.0 |
The error level when converting from object was changed from E_NOTICE to E_WARNING.
|
Example #1 intval() examples
The following examples are based on a 64 bit system.
<?php
echo intval(42), PHP_EOL; // 42
echo intval(4.7), PHP_EOL; // 4
echo intval('42'), PHP_EOL; // 42
echo intval('+42'), PHP_EOL; // 42
echo intval('-42'), PHP_EOL; // -42
echo intval(042), PHP_EOL; // 34
echo intval('042'), PHP_EOL; // 42
echo intval(1e10), PHP_EOL; // 10000000000
echo intval('1e10'), PHP_EOL; // 10000000000
echo intval(0x1A), PHP_EOL; // 26
echo intval('0x1A'), PHP_EOL; // 0
echo intval('0x1A', 0), PHP_EOL; // 26
echo intval(42000000), PHP_EOL; // 42000000
echo intval(420000000000000000000), PHP_EOL; // -4275113695319687168
echo intval('420000000000000000000'), PHP_EOL; // 9223372036854775807
echo intval(42, 8), PHP_EOL; // 42
echo intval('42', 8), PHP_EOL; // 34
echo intval(array()), PHP_EOL; // 0
echo intval(array('foo', 'bar')), PHP_EOL; // 1
echo intval(false), PHP_EOL; // 0
echo intval(true), PHP_EOL; // 1
?>Note:
The
baseparameter has no effect unless thevalueparameter is a string.