PHP intval() function
Description
Returns the integer value of var, using the specified base for the conversion (the default is base 10).
Syntax
PHP intval() function has the following syntax.
int intval ( mixed $var [, int $base = 10 ] )
Parameter
- var - The scalar value being converted to an integer
- base - The base for the conversion
If base is 0, the base used is determined by the format of var:
- if string includes a "0x" (or "0X") prefix, the base is taken as 16 (hex); otherwise,
- if string starts with "0", the base is taken as 8 (octal); otherwise, the base is taken as 10 (decimal).
Return
The integer value of var on success, or 0 on failure.
Example
Find out the int value
<?php//from ww w .j a v a 2s . c om
echo intval(2) . "\n"; // 2
echo intval(4.1) . "\n"; // 4
echo intval('4') . "\n"; // 4
echo intval('+42') . "\n"; // 42
echo intval('-42') . "\n"; // -42
echo intval(042) . "\n"; // 34
echo intval('04') . "\n"; // 4
echo intval(1e10) . "\n"; // 1410065408
echo intval('1e10') . "\n"; // 1
echo intval(0x1A) . "\n"; // 26
echo intval(42, 8) . "\n"; // 42
echo intval('42', 8) . "\n"; // 34
echo intval(array()) . "\n"; // 0
echo intval(array('foo', 'bar')) . "\n"; // 1
?>
The code above generates the following result.