intval()

Description

This function returns the int value of an expression.

Syntax


int intval( mixed $expr [, int $base = 10] )

Parameters

$expr
Expression for which the int value should be returned. The passed expression can be any type except object.
$base = 10
If $expr is a string, $base can be used to convert the expression to a particular base. If $expr is not a string, $base is ignored. If $base is passed as 0, the following rules apply:

  • If $expr starts with “0x” or “0X”, the base is assumed to be 16 (hex)
  • If $expr starts with “0”, the base is assumed to be 8 (octal)
  • Otherwise, the base is assumed to be 10 (decimal)

Return Value

int
The passed expression as an int.

Examples

1. Evaluate Various Expression Types as int

This example passes literals of various types to intval() and displays the returned value.

<?php
echo 'intval(0):           ' . intval(0)           . "\n";
echo 'intval(1.23):        ' . intval(1.23)        . "\n";
echo 'intval(""):          ' . intval("")          . "\n";
echo 'intval("abc"):       ' . intval("abc")       . "\n";
echo 'intval("12abc"):     ' . intval("12abc")     . "\n";
echo 'intval("12.345abc"): ' . intval("12.345abc") . "\n";
echo 'intval("abc12.345"): ' . intval("abc12.345") . "\n";
echo 'intval(true):        ' . intval(true)        . "\n";
echo 'intval(false):       ' . intval(false)       . "\n";
echo 'intval([]):          ' . intval([])          . "\n";
echo 'intval([2,3]):       ' . intval([2,3])       . "\n";
?>

The output of the program above produces the output below. Passed numbers return the expected values. Passed strings return 0 unless the leading part of the string is a valid numeric. Boolean true is returned as 1 and Boolean false is returned as 0. Empty arrays are returned as 0 and non-empty arrays are returned as 1.

intval(0):           0
intval(1.23):        1
intval(""):          0
intval("abc"):       0
intval("12abc"):     12
intval("12.345abc"): 12
intval("abc12.345"): 0
intval(true):        1
intval(false):       0
intval([]):          0
intval([2,3]):       1

2. More String to Number Conversions

This example shows some more string to float conversions. Hyphens, dollar signs, commas, and exponential notation are examined.

<?php
echo 'intval("-12.6abc"):   ' . intval("-12.6abc")   . "\n";
echo 'intval("-12.6e3abc"): ' . intval("-12.6e3abc") . "\n";
echo 'intval("-12e3abc"):   ' . intval("-12e3abc")   . "\n";
echo 'intval("$123.45"):    ' . intval("$123.45")    . "\n";
echo 'intval("-123,456"):   ' . intval("-123,456")   . "\n";
?>

The output of the example above follows. A leading hyphen is correctly interpreted as a negative number. Trailing exponential notation (e.g. “e3”) is not considered when parsing the integer portion of the string.

Embedded dollar signs and commas are interpreted as garbage just like any other letter.

intval("-12.6abc"):   -12
intval("-12.6e3abc"): -12
intval("-12e3abc"):   -12
intval("$123.45"):    0
intval("-123,456"):   -123

3. Evaluate Expressions with Base

This example shows the result of using the $base parameter. In lines 2-4 the value “27” is interpreted as decimal (the default), octal, and hex. In lines 6-8, the value 0 is passed as an argument for the $base parameter. This causes intval() to use the first character of the string to determine its base (“0” as octal, “0x” as hex). In lines 10-13, a numeric value is passed as $expr rather than a string. In these cases, the $base is ignored. However, you can still pass octal and hex literals as shown on lines 11 and 13, respectively.

<?php
echo 'intval("27", 10):  ' . intval("27", 10)  . "\n";
echo 'intval("27", 8):   ' . intval("27", 8)   . "\n";
echo 'intval("27", 16):  ' . intval("27", 16)  . "\n\n";

echo 'intval("27", 0):   ' . intval("27", 0)   . "\n";
echo 'intval("027", 0):  ' . intval("027", 0)  . "\n";
echo 'intval("0x27", 0): ' . intval("0x27", 0) . "\n\n";

echo 'intval(27, 8):     ' . intval(27, 8)     . "\n";
echo 'intval(027):       ' . intval(027)       . "\n";
echo 'intval(27, 16):    ' . intval(27, 16)    . "\n";
echo 'intval(0x27):      ' . intval(0x27)      . "\n";
?>

The output of the program above is shown below. Lines 1-3 show the value of “27” interpreted as a decimal, octal, and hex value. Lines 5-7 use the $base = 0 to display the values of “27” decimal, “27” octal, and “27” hex. Lines 9-12 show that the $base argument is ignored if the $expr argument is not a string.

intval("27", 10):  27
intval("27", 8):   23
intval("27", 16):  39

intval("27", 0):   27
intval("027", 0):  23
intval("0x27", 0): 39

intval(27, 8):     27
intval(027):       23
intval(27, 16):    27
intval(0x27):      39

4. Evaluate an Object as a int

Here we attempt to pass an object to intval(). This generates an error message but returns the value 1.

<?php
echo 'intval(new stdClass): ' . intval(new stdClass) . "\n";
?>

The output is shown below.

PHP Notice:  Object of class stdClass could not be converted to int in /home/charlie/php/intval3.php on line 2
intval(new stdClass): 1

See Also

References