is_int() checks the variable type. A form value such as '25' is a string even though it contains digits, so validate or convert external input before expecting is_int() to return true.
$a = 25;
$b = '25';
var_dump(is_int($a)); // true
var_dump(is_int($b)); // false
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
<?php
$var=5;
echo is_int($var); // Output 1
echo "<br>";
$var='5';
echo is_int($var); // No Output
echo "<br>";
$var=5.8;
echo is_int($var); // No output (for Float)
echo "<br>";
$var=+5;
echo is_int($var); // Output 1
echo "<br>";
$var=-5;
echo is_int($var); // Output 1
echo "<br>";
$var='a123';
echo is_int($var); // No output
echo "<br>";
?>
$var="123";
if(is_int($var)){
echo " this is an integer ";
}else{
echo " this is not an integer ";}
Above code will say this is not an integer$var=$_POST['t1'];
if(is_int($var)){
echo " this is an integer ";
}else{
echo " this is not an integer ";
}
We can create an form to post data to the above code like this
<form method=post action=is_int1.php>
<input type=text name=t1>
<input type=submit value=submit>
In the above form if we post an integer then also is_int will return false.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| gen | 13-07-2012 |
| how about is not numeric??? | |