I just wonder why php have a function like is_int()
. Documentation says that
To test if a variable is a number or a numeric string (such as form input, which is always a string)
I am sure that no one will ever have to check the type of a variable that is statically assigned. So someone please tell me will there be any situation, where the function is_int()
really useful ?
complete understding soluation....
this is code
<?php
$values = array(23, "23", 23.5, "23.5", null, true, false);
foreach ($values as $value) {
echo "is_int(";
var_export($value);
echo ") = ";
var_dump(is_int($value));
}
?>
this is output
is_int(23) = bool(true)
is_int('23') = bool(false)
is_int(23.5) = bool(false)
is_int('23.5') = bool(false)
is_int(NULL) = bool(false)
is_int(true) = bool(false)
is_int(false) = bool(false)
This function can be useful in the following scenario,
$a = 5;
//some functionality
function passbyref(&$b)
{
$b = "45";
}
passbyref($a);
if(is_int($a))
{
print "Yes it is int";
}
It finds whether the type of the given variable is integer
or not.
This is the example I found :
$values = array(23, "23", 23.5, "23.5", null, true, false);
foreach ($values as $value) {
echo "is_int(";
var_export($value);
echo ") = ";
var_dump(is_int($value));
}
The above example will output:
is_int(23) = bool(true)
is_int('23') = bool(false)
is_int(23.5) = bool(false)
is_int('23.5') = bool(false)
is_int(NULL) = bool(false)
is_int(true) = bool(false)
is_int(false) = bool(false)
To understand more : Click here
Consider a user form input:
$_POST['integer'] = "3";
$integer = +$_POST['integer']; // convert to a number
var_dump(is_int($integer)); // bool(true)
$_POST['float'] = "3.5";
$float = +$_POST['float']; // convert to a number
var_dump(is_int($float)); // bool(false)