is_intCHECK if a given
VARIABLE is or is not a numeric
INTEGER VALUE.
Aliases:
is_integer and
is_long.
is_integer
CHECK if a given VARIABLE is or is not a numeric INTEGER VALUE.
Aliases: is_int and is_long.
is_long
CHECK if a given VARIABLE is or is not a numeric INTEGER VALUE.
Aliases: is_integer and is_int.
This function returns a BOOLEAN value when it tests the following variable types:
STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.
One way to visualize the test result is to use var_dump, however, it is better to use one for the if options as a conditional evaluation framework.
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Aliases:
is_int, is_integer and is_long
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool is_int ( mix $var );
or
bool is_integer ( mix $var );
or
bool is_long ( mix $var );
where,
$var = VARIABLE to test
?>
$var
The variable to test.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
var_dump
to display the variable as is
- - - - - - - - - - - - - - - - - - - - - - - */
var_dump($_SERVER['REMOTE_ADDR']);
echo '<br><br>';
var_dump($_SERVER['HTTP_USER_AGENT']);
echo '<br><br>';
var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo '<br><br>';
var_dump(M_E);
echo '<br><br>';
$var01e = 1.6e-19;
var_dump($var01e);
$var01f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
echo '<br><br>';
var_dump($var01f);
$var01g = NULL;
echo '<br><br>';
var_dump($var01g);
echo '<br><br>';
$var01h = 123456;
var_dump($var01h);
?>
RESULT
string(3) "::1"
string(114) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
string(35) "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
float(2.718281828459)
float(1.6E-19)
array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
NULL
int(123456)
For you, some results may be different.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is INTEGER
The same as: is_integer or is_long
- - - - - - - - - - - - - - - - - - - - - - - */
$var02a = $_SERVER['REMOTE_ADDR'];
$var02b = $_SERVER['HTTP_USER_AGENT'];
$var02c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var02d = M_E;
$var02e = 1.6e-19;
$var02f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var02g = NULL;
$var02h = 123456;
$var02i = '123456';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02a))
// if(is_integer($var02a))
// if(is_log($var02a))
var_dump($va02a);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02b))
// if(is_integer($var02b))
// if(is_long($var02b))
var_dump($va02b);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02c))
// if(is_integer($var02c))
// if(is_long($var02c))
var_dump($va02c);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02d))
// if(is_integer($var02d))
// if(is_long($var02d))
var_dump($va02d);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02e))
// if(is_integer($var02e))
//if(is_long($var02e))
var_dump($var02);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02g))
// if(is_integer($var02g))
// if(is_long($var02g))
var_dump($var02g);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02h))
// if(is_integer($var02h))
// if(is_long($var02h))
var_dump($var02h);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var02i))
// if(is_integer($var02i))
// if(is_long($var02i))
var_dump($var02i);
echo '<br>IS INTEGER<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
IS INTEGER
IS INTEGER
IS INTEGER
IS INTEGER
IS INTEGER
IS INTEGER
int(123456)
IS INTEGER
IS INTEGER
As you can see in the results above, using only one statment for the if loop, the result for some variables can be displayed wrongly.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
an user-defined function
to display if the variable
is INTEGER
The same as: is_int or is_long
- - - - - - - - - - - - - - - - - - - - - - - */
$var03a = $_SERVER['REMOTE_ADDR'];
$var03b = $_SERVER['HTTP_USER_AGENT'];
$var03c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var03d = M_E;
$var03e = 1.6e-19;
$var03f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var03g = NULL;
$var03h = 123456;
$var03i = '123456';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03a))
{
var_dump($var03a);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03a);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03b))
{
var_dump($var03b);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03b);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03e))
{
var_dump($var03d);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03d);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03e))
{
var_dump($var03e);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03e);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03f))
{
var_dump($var03f);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03f);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03g))
{
var_dump($var03g);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03g);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03h))
{
var_dump($var03h);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03h);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_int($var03i))
{
var_dump($var03i);
echo '<br>is an INTEGER<br><br>';
}
else
{
var_dump($var03i);
echo '<br>is NOT an INTEGER<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
is NOT INTEGER
string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is NOT INTEGER
string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is NOT INTEGER
float(2.718281828459)
is NOT INTEGER
float(1.6E-19)
is NOT INTEGER
array(2) {
["c"]=>
int(299792458)
["G"]=>
float(6.67428E-11)
}
is NOT INTEGER
NULL
is NOT INTEGER
int(123456)
is INTEGER
string(6) "123456"
is NOT INTEGER
There is a simpler way of obtaining whether a variable is an INTEGER or not.
This will be seen the moment we study the user-defined functions.