(PHP 3, PHP 4, PHP 5)

is_int -- 检测变量是否是整数


bool is_int ( mixed var )

如果 varinteger 则返回 TRUE,否则返回 FALSE

注: 若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric()

参见 is_bool()is_float()is_integer()is_numeric()is_string()is_array()is_object()

add a note add a note User Contributed Notes
14-Jul-2006 09:00
is_int() and class objects

If you wish to ask the question "Does this object contain an integer value?" for a class object, is_int() is not the function for you.  is_int() does not work as may be expected with class objects, I'm guessing because it tries to evaluate the object itself rather than the value of the object.

For example, assuming that $this->myid = 2, the following snippet will result in the output "false"....

if(is_int((string)$this->myid)) print "true";
else print "false";

Neither will is_numeric work() as required; because it will return "true" for non-integers, such as 3.1, 6.7 and 0 (discussion about 0 as an integer referred to /dev/null for these purposes).

Instead, use the following,which works exactly as required...

if(is_numeric($this->myid) and intval($this->myid))
Ender at soldat dot nl
16-Feb-2006 07:38
Be aware that is_numeric (mentioned in this article as the proper way to validate string numbers) also allows numbers in scientific and hexadecimal annotation. Thus DO NOT USE that function to validate user input that will be used as id number for in a query for example, this could cause mysql errors. Use ctype_digit instead.
ludvig dot ericson at gmail dot com
07-Jan-2006 05:00
I would like to say that is_int() is pretty helpfull when looking for neat proper ways to check functions that return either integers or booleans (false) on failure (strpos, socket_select, etc.)
function mySelect() {
$ret = socket_select($someSockets, $o = array(), $e = array(), 0);
   if (!
return true;    // Return true if the function proceeded as expected.
The point of doing this is that if you put this in a while() loopo, you'll break it when the select fails.
while (mySelect());

Hope you get the point
 - toxik
19-May-2005 02:46
function precisionNumero($numero, $precision, $noDevuelveNulo) {
   if ( (isset($numero)) && (is_numeric($numero)) ) { //Es un nmero
       if ( ((int)$numero) == ((float)$numero) ) { //Entero
       else { //Real
           if ((substr($numero,0,1)) ==".") {//Real que comienza por .
               sscanf ($numero,$patronPrecision, $decimal);
           else { //real estandar
               sscanf ($numero, $patronPrecision, $entero,$decimal);
       for ($i=strlen($decimal); $i<$precision ; $i++) {
   else { // No es un numero valido
       if ($noDevuelveNulo) {
           for ($i=0; $i<$precision ; $i++) {
       else return NULL;
   return ($entero.".".$decimal);

precisionNumero("1.2.3", 2, true) returns 0.00
precisionNumero("1.23", 3, true) returns 1.230
precisionNumero(".23", 5, true) returns 0.23000
precisionNumero("1.", 7, true) returns 1.0000000
precisionNumero("1.2.3", 3, false) returns NULL
precisionNumero("0.0", 3, false) returns 0.000
berndt at www dot michael - berndt dot de
07-May-2005 02:57
checks if the divisibility is true with is_int ()
phpContrib (A T) esurfers d o t c o m
07-Nov-2003 04:42
// Just to be pedantic... ;)
// extreme-ints (!) like this will not work with previous function:

Test ("10.01e2" );
// 10.01e2 is 1001

// This one:

function myIsInt($x) {
   return ( is_numeric ($x ) ?  intval(0+$x ) ==  $x  :  false );

// will work even on these paranoid cases...

Note that:

   0+$x => converts object to float
   intval converts float to int
   == converts string to float
so this is exactly like

   return (is_numeric ($x ) ?  (float)((int)((float)$x))  === (float)$x  :  false) ;

or with one conversion to float less...

function real_is_intValued($var) {
   if(is_numeric($var)) {
       return ((float)(int)$var)===$var;
   return FALSE;
lclkk at urbanvagabond dot net
16-Sep-2003 10:24
I think the function below is a robust test for integers working on all datatypes. It works by first checking that a number can be evaluated numerically, and then secondly that the integer evaluation matches the original number.

Test cases are included.

function myIsInt ($x) {
   return (
is_numeric($x) ? intval($x) == $x : false);

Test($x) {
"$x is " . ( myIsInt($x) ? ('an integer. The integer value is ' . intval($x)) : 'not an integer.');

"These should be integers...\n";

"\nThese should not be integers...\n";
Test(true); // watch out, this displays as '1'
Test(""); // empty string
Test(dir('.')); // object
gabe at websaviour dot com
15-Jul-2003 02:08
Although this can be inferred from the documentation, beware of numeric strings.  I ran into the problem in a MySQL app where I would either SELECT an INT PRIMARY KEY or INSERT a new record and use mysql_insert_id() to get the KEY before continuing onto the new section. 

I used is_int() to make sure the subsequent queries wouldn't break when using the key variable.  Unfortunately I failed to realize that while mysql_insert_id() returns an int, mysql_result() always returns a string even if you are SELECTing from an INT field.

Spent at least 30 minutes trying to figure out why existing records weren't getting linked, but new records would link fine.  I ended up using intval() on mysql_result() to make sure subsequent queries still always work.
phpguru at gmx dot ch
05-Jul-2003 11:50
To Logan:

There's also a simple non-regexp way to convert a (form) value into an integer if it consists of numbers only - although with a trap (see below):

if ($_POST["number"] == (int)$_POST["number"]) $_POST["number"] = (int)$_POST["number"];

The "traps" (or "side effects") appear with values like "" (empty string) and false (boolean), which are converted to 0 (integer). But in certain cases this might be desirable or/and usefull ;-)

Solutions like

if (($_POST["number"] + 1 - 1) == $_POST["number"]) ...

falls into the same category.
mark at g33kz dot co dot uk
18-Jun-2003 08:44
Or you could just use is_numeric()

I have a file called input.php which I run at the beginning of all my scripts which makes sure all my input numbers are converted to integers automatically.

if ($_GET) {
  foreach ($_GET as $k => $v) {
   $_GET[$k] = trim (stripslashes ($v));
   if (is_numeric ($v)) {
     $_GET[$k] = intval ($v);
if ($_POST) {
  foreach ($_POST as $k => $v) {
   $_POST[$k] = trim (stripslashes ($v));
   if (is_numeric ($v)) {
     $_POST[$k] = intval ($v);
if ($_COOKIE) {
  foreach ($_COOKIE as $k => $v) {
   $_COOKIE[$k] = trim (stripslashes ($v));
   if (is_numeric ($v)) {
     $_COOKIE[$k] = intval ($v);
logan at logannet dot net
11-Feb-2003 07:42
[[Editors note: Or you can simply use is_numeric()]]

Some people have offered their ways to find out if a string from a form is an integer or not, here's my way:

if(ereg("^[0-9]+$", $_POST["number"])) $_POST["number"] = (int)$_POST["number"];

In psuedo code:
if you are a string full of numbers then convert yourself to an integer

So instead of just checking if its a string full of numbers you check and then convert it, which means you can use the standard is_int. You can also do:

if(ereg("^[0-9]+$", $_POST["number"])) $_POST["number"] += 0;

I think the first way i mentioned is better because your coding what you want to do, rather than the second way that uses a side effect of adding 0 to convert the string.

The first way also may make your code ever so slightly faster (nothing noticeable) as php does not need to add 0 to the number after it converts it.

Also note an integer is full numbers (1, 2, 3 etc) not decimal numbers (1.1, 2.4, 3.7 etc), to convert decimal numbers you could use something like:

if(ereg("^[.0-9]+$", $_POST["number"])) $_POST["number"] = (float)$_POST["number"];


if(ereg("^[.0-9]+$", $_POST["number"])) $_POST["number"] += 0;

But note that these would not work with is_int(), because they are not integers.