intval

(PHP 4, PHP 5)

intvalGet the integer value of a variable

Description

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

Returns the integer value of var, 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_NOTICE level error and return 1.

Parameters

var

The scalar value being converted to an integer

base

The base for the conversion

Note:

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 Values

The integer value of var 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.

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(428);                   // 42
echo intval('42'8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo''bar'));     // 1
?>

Notes

Note:

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

Changelog

Version Description
5.1.0 Throws E_NOTICE and returns 1, when an object is passed to var.

See Also

Коментарии

Автор:
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
2000-08-12 04:38:01
http://php5.kiev.ua/manual/ru/function.intval.html
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. :)
2004-02-07 06:56:07
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
You guys are going to love this.  I found something that I found quite disturbing.

$test1 = intVal(1999);

$amount = 19.99 * 100;
$test2 = intVal($amount);
$test3 = intVal("$amount");

echo $test1 . "<br />\n";
echo $test2 . "<br />\n";
echo $test3 . "<br />\n";

expected output:
1999
1999
1999

actual output
1999
1998
1999

Appears to be a floating point issue, but the number 1999 is the only number that I was able to get to do this.  19.99 is the price of many things, and for our purpose we must pass it as 1999 instead of 19.99.
2006-01-16 15:21:54
http://php5.kiev.ua/manual/ru/function.intval.html
As addendum, the "if ($int > 0)" check in the encode function is redundant. It doesn't do anything bad to keep it in since it will always be true when reaching that point, but it's a meaningless conditional this way. It's a remnant from when I tried to write the function in terms of bitshifts, which could lead to negative ints when shifting if the 32nd bit was set (instead of always padding with 0's when using >> it pads with 1's leading to negative ints).
2006-03-07 01:48:05
http://php5.kiev.ua/manual/ru/function.intval.html
Here is a really useful undocumented feature:

You can have it automatically deduce the base of the number from the prefix of the string using the same syntax as integer literals in PHP ("0x" for hexadecimal, "0" for octal, non-"0" for decimal) by passing a base of 0 to intval():

<?php
echo intval("0x1a"0), "\n"// hex; prints "26"
echo intval("057"0), "\n"// octal; prints "47"
echo intval("42"0), "\n"// decimal; prints "42"
?>
2009-06-21 13:35:32
http://php5.kiev.ua/manual/ru/function.intval.html
Be careful :

<?php
$n
="19.99";
print 
intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999
?>
2010-12-16 10:31:03
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
The behaviour of intval() is interesting when supplying a base, and you better check your intval base-based expressions, as it is counter-intuitive.
As the example shows
<?php
  intval
('42'8); // => 34
 
intval(428);   // => 42 !
?>
PHP considers the 42 as being already an integer, and doesn't apply any conversion. And supplying
<?php
  intval
(498);  // => 49 !
?>
produces no error and no warning.
2010-12-27 22:53:37
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
Not mentioned elsewhere: intval(NULL) also returns 0.
2011-11-12 01:50:08
http://php5.kiev.ua/manual/ru/function.intval.html
if you want to take a number from a string, no matter what it may contain, here is a good solution:

<?php
function int($s){return(int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s);}

echo 
int('j18ugj9hu0gj5hg');
//output: 18
?>
this example returns an int, so it will follow the int rules, and has support for negative values.

<?php
function int($s){return($a=preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s))?$a:'0';}

echo 
int('j-1809809808908099878758765ugj9hu0gj5hg');
//output: -1809809808908099878758765
?>

this one returns a string with just the numeric value.
it also supports negative values.

the latter is better when you have a 32 bit system and you want a huge int that is higher than PHP_MAX_INT.
2013-03-06 11:58:26
http://php5.kiev.ua/manual/ru/function.intval.html
Do not use intval() when you really want round(). This is due to how PHP handles precision. 

echo number_format(8.20*100, 20), "<br />";
echo intval(8.20*100), "<br />";
echo floor(8.20*100), "<br />";
echo round(8.20*100), "<br />";

819.99999999999988631316
819 
819
820
2013-04-25 20:27:05
http://php5.kiev.ua/manual/ru/function.intval.html
beware:

<?php

   
// observe the following
   
echo intvalstrval( -0.0001 ) ); // 0
   
echo intvalstrval( -0.00001 ) ); // -1

   // this is because
   
echo strval( -0.0001 ); // -.0001
   
echo strval( -0.00001 ); // -1.0E-5

   // thus beware when using
   
function trunc2_bad$n ) {
      return 
intvalstrval$n 100 ) / 100 );
   }

   
// use this instead
   
function trunc2_good$n ) {
      return 
intvalfloatvalstrval$n 100 )  ) / 100 );
   }

?>
2015-07-11 07:45:41
http://php5.kiev.ua/manual/ru/function.intval.html
It seems intval is interpreting valid numeric strings differently between PHP 5.6 and 7.0 on one hand, and PHP 7.1 on the other hand.

<?php
echo intval('1e5');
?>

will return 1 on PHP 5.6 and PHP 7.0,
but it will return 100000 on PHP 7.1.
2017-01-27 17:35:30
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
The binary notation is NOT supported until php7.2
<?php
                         
// PHP <7.2 | PHP >=7.2
echo intval(0b11);       //    3     |     3
echo intval(-0b11);      //   -3     |    -3
echo intval('0b11');     //    0     |     0
echo intval('-0b11');    //    0     |     0
echo intval('0b11'0);  //    0     |     3
echo intval('-0b11'0); //    0     |    -3
?>
2018-04-21 15:28:01
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
PHP 7.2 

$test = intval(150.20*100); //15019
$test2 = intval(15020); //15020
$test3 = intval(15020.0); //15020
$test4 = 150.20*100; //15020.0
2020-02-12 19:14:32
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
As a warning, do not use this function alone for input validation. 

Vulnerable example:
<?php
if(isset($_GET['id']) && intval($_GET['id']) > 0){
    echo 
$id;
}
?>

The following requests would pass this filter:

/page.php?id=10
/page.php?id=10oops
/page.php?id=10<script>alert(1)</script>
/page.php?id=1' OR '1'='1
/page.php?id[]=<script>alert(1)</script>

Instead use the is_numeric() function for integer validation:

<?php
echo intval("10oops"); // 10
echo is_numeric("10oops");  // false
?>

Secure example:
<?php
if(isset($_GET['id']) && is_numeric($_GET['id']) && intval($_GET['id']) > 0){
    echo 
$id;
}
?>
2020-05-15 18:46:09
http://php5.kiev.ua/manual/ru/function.intval.html
Автор:
Warning: intval() parses scientific notation, like "12.3e7".

This comes out of the blue when you use intval() to cut the first integer value from a string; at first glance there's nothing wrong with it, if you have "123.jeff" it will give you 123, but in the rare case of parsing something that has a second segment with a hex number, you can easily run into this. (Let's not start the "why would you parse a string like that" argument.)

So if you're not prepared, these results may surprise you:

    intval("123.ee-2") - gives you 123
    intval("123.e2-e") - gives you 12300
    intval("123.a2-e") - gives you 123
    intval("123.e-22") - gives you 0
    intval("123.e-a2") - gives you 123
    intval("123.e-2a") - gives you 1
    intval("123.2e2a") - gives you 12320
    intval("123.22e2") - gives you 12322
    intval("123.22ea") - gives you 123

Again, this is somewhat expected behaviour once you know that scientific notation is interpreted by it. But it looks like a less-than-widely known fact and I only faced this issue after 20+ years of php.
2024-01-24 11:28:46
http://php5.kiev.ua/manual/ru/function.intval.html

    Поддержать сайт на родительском проекте КГБ