w3resource

PHP: is_int() function

Description

The is_int () function is used to test whether the type of the specified variable is an integer or not.

Version:

(PHP 4 and above)

Syntax:

is_int(var_name)

Parameter:

Name Description Required /
Optional
Type
var_name The variable being checked Required Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:

TRUE if var_name is an integer, FALSE otherwise.

Value Type: Boolean

Example:

<?php
$var_name1=678;
$var_name2="a678";
$var_name3="678";
$var_name4=999;
$var_name5=698.99;
$var_name6=array("a1","a2");
$var_name7=+125689.66;
if (is_int($var_name1))
{
echo "$var_name1 is Integer<br>" ;
}
else
{
echo "$var_name1 is not an Integer<br>" ;
}
if (is_int($var_name2))
{
echo "$var_name2 is Integer<br>" ;
}
else
{
echo "$var_name2 is not Integer<br>" ;
}
$result=is_int($var_name3);
echo "[ $var_name3 is Integer? ]" .var_dump($result)."<br>";
$result=is_int($var_name4);
echo "[ $var_name4 is Integer? ]" .var_dump($result)."<br>";
$result=is_int($var_name5);
echo "[ $var_name5 is Integer? ]" .var_dump($result)."<br>";
$result=is_int($var_name6);
echo "[ $var_name6 is Integer? ]" .var_dump($result)."<br>";
$result=is_int($var_name7);
echo "[ $var_name7 is Integer? ]" .var_dump($result);
?>

Output:

678 is Integer
a678 is not Integer
bool(false)  [ 678 is Integer? ]
bool(true)  [ 999 is Integer? ]
bool(false)  [ 698.99 is Integer? ]
bool(false)  [ Array is Integer? ]
bool(false)  [ 125689.66 is Integer? ]

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: is_float
Next: is_integer



PHP: Tips of the Day

SQL injection that gets around mysql_real_escape_string()

Consider the following query:

$iId = mysql_real_escape_string("1 OR 1=1");    
$sSql = "SELECT * FROM table WHERE id = $iId";

mysql_real_escape_string() will not protect you against this. The fact that you use single quotes (' ') around your variables inside your query is what protects you against this. The following is also an option:

$iId = (int)"1 OR 1=1";
$sSql = "SELECT * FROM table WHERE id = $iId";

Ref : https://bit.ly/32q3bJ7