PHP intval() function
In this chapter you will learn:
- Description for PHP intval() function
- Syntax for PHP intval() function
- Parameter for PHP intval() function
- Return value for PHP intval() function
- Example - Find out the int value
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 j ava2s. c o m*/
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.
Next chapter...
What you will learn in the next chapter:
- Description for PHP is_array(value) function
- Syntax for PHP is_array(value) function
- Parameter for
- Return value for
- Example - Is it an array
Home » PHP Tutorial » PHP Data Type Functions