search for in the  
<import_request_variablesis_array>
Last updated: Thu, 19 May 2005

intval

(PHP 3, PHP 4, PHP 5)

intval -- Get the integer value of a variable

Description

int intval ( mixed var [, int base] )

Returns the integer value of var, using the specified base for the conversion (the default is base 10).

Parameters

var

The scalar value being converted to an integer

base

The base for the conversion (default is base 10)

Return Values

The integer value of var on success, or 0 on failure. Empty arrays and objects return 0, non-empty arrays and objects 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.

Examples

Example 1. intval() examples

The following examples are based on a 32 bit system.

<?php
echo intval(42);                      // 42
echo intval(4.2);                    // 4
echo intval('42');                    // 42
echo intval('+42');                  // 42
echo intval('-42');                  // -42
echo intval(042);                    // 34
echo intval('042');                  // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);  // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                  // 42
echo intval('42', 8);                // 34
?>

Notes

Note: The base parameter has no effect unless the var parameter is a string.



User Contributed Notes
intval
23-May-2004 10:38
When trying to read 32bit values (32 bit limitation depends on the word size of your machine)  from a string that is represented in hex with the high bit set,
eg. F9833234

intval returns -1. The reason is the sign bit being set. This number is larger than can be saved in a signed int.

The way around this is to read the value in using two calls to intval.

eg.

$val = intval(substr($str,0,4), 16); // read high 16 bit word
$val <<= 16; // shift hi word correct position
$val |= intval(substr($str, 4, 4), 16); //  read low 16 bit word
tuxedobob at mac dot com
07-Feb-2004 06:56
Sometimes intval just won't cut it. For example if you want to use an unsigned 32-bit int and need all 32 bits. Recently, I wrote a little script that took and integer and converted it to an IP address. After realizing I couldn't just mod the whole thing, since the sign bit throws it off (and compensating for that), we ran into a problem where if it was entered into a form, the value somehow wasn't converted to an integer properly, at least not implicitly. The solution for this, and the way I recommend converting a string to an integer, is:

$num = $num + 0;

and PHP will leave your number alone; it'll just know it's a number. Such is the fun of a loosely-typed language. :)
Robin Y. Millette http://rym.waglo.com
15-May-2003 03:29
Rob_Kohr at no_need_to_email dot me dot com
11-Nov-2002 12:24   

[snip]

$var=intval("122.5");
//$var==122

This is nice if you want to turn a double into an int automatically rounding down

Hum, I had a bug earlier today, involving ===. Coming from a c++ background, I can't help testing for types. I was using floor() to get an integer from a division by 2, and comparing that to a known integer from a for loop. Well, first I changed the === to == because the test would always be false otherwise. Next, I looked up this function, and converted most of my floor() calls to intval() calls, because I really meant to get an int, and not a float with no decimal part. So I have to disagree with the editor note here. Oh, and I'm comfortably back to using ===.
hrpeters at NOJUNK dot gmx dot net
30-Dec-2002 10:06
Leading plus signs (e.g. +5) are allowed, altough it isn't mentioned explicitly.

I found this very helpful, when dealing with relative date and time
cleong at letstalk dot com
01-Oct-2001 07:21
intval() handles overflow differently depending on the type of the argument.

intval('10000000000') = 2147483647
intval(1e10) = 1410065408

intval(float) yields essentially non-defined result when the argument is beyond the range of int.
spectator at nanum dot net
11-May-2001 10:30
For hexadecimal value...
(Ex, RGB color value => each R,G,B integer value)
---------------------------------------

<?
$rgb
= '#E7A3EF';
$r = intval(substr($rgb,1,2),16);
$g = intval(substr($rgb,3,2),16);
$b = intval(substr($rgb,5,2),16);
?>
RGB : <?= $rgb ?><br>
R : <?= $r ?><br>
G : <?= $g ?><br>
B : <?= $b ?><br>

---------------------------------------
=> RGB : #E7A3EF
R : 231
G : 163
B : 239
rasmus at SPAMCAN dot lvcm dot com
19-Apr-2001 12:13
Remember that INTVAL can only work with values between -2147483648 and 2147483647. Anything smaller or larger (respectively) and INTVAL will truncate it.

thus:

<?php
$value
= 3010010001;
$newval = intval ($value);
echo
$newval;
?>

will return:

2147483647
zak at php dot net
12-Aug-2000 03:38
intval converts doubles to integers by truncating the fractional component of the number.

When dealing with some values, this can give odd results.  Consider the following:

print intval ((0.1 + 0.7) * 10);

This will most likely print out 7, instead of the expected value of 8.

For more information, see the section on floating point numbers in the PHP manual (http://www.php.net/manual/language.types.double.php)

Also note that if you try to convert a string to an integer, the result is often 0.

However, if the leftmost character of a string looks like a valid numeric value, then PHP will keep reading the string until a character that is not valid in a number is encountered.

For example:

"101 Dalmations" will convert to 101

"$1,000,000" will convert to 0 (the 1st character is not a valid start for a number

"80,000 leagues ..." will convert to 80

"1.4e98 microLenats were generated when..." will convert to 1.4e98

Also note that only decimal base numbers are recognized in strings.

"099" will convert to 99, while "0x99" will convert to 0.

One additional note on the behavior of intval.  If you specify the base argument, the var argument should be a string - otherwise the base will not be applied.

For Example:

print intval (77, 8);  // Prints 77
print intval ('77', 8); // Prints 63

<import_request_variablesis_array>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT