I would like the code below to return ‘Int’ the first time and ‘Not int’ the second time. Unfortunately it returns ‘Not int’ twice instead.
How can I fix this?
<?php
$test1='1';
if(is_int($test1)){
echo "Int";
}else{
echo "Not int";
}
echo "\n";
$test2='1a';
if(is_int($test2)){
echo "Int";
}else{
echo "Not int";
}
?>
By wrapping the number in quotation marks
'1', you are declaring a string.Instead you got to use
$test1 = 1;.By using the PHP
ctype_digit()function, you can check if a string only contains digits.You could also use the
is_numeric()function, which also returns true if the string contains a exponential part like+0123.45e6or a hex value0xFF.