// to check the input integer validation we can use is_int() function
Syntax:
is_int(parameter);
$x = 10; //returns true
$x = "123"; //returns false
$x = 12.365; //returns false
$x = "ankur"; //returns false
is_int($x);
// Check if variable is int
$id = "1";
if(!intval($id)){
throw new Exception("Not Int", 404);
}
else{
// this variable is int
}
$a = 5; //returns true
$a = "5"; //returns false
$a = 5.3; //returns false
is_int($a);
<?php
// combination of is_int and type coercion
// FALSE: 0, '0', null, ' 234.6', 234.6
// TRUE: 34185, ' 34185', '234', 2345
$mediaID = 34185;
if( is_int( $mediaID + 0) && ( $mediaID + 0 ) > 0 ){
echo 'isint'.$mediaID;
}else{
echo 'isNOTValidIDint';
}
?>
is_int(mixed $value): bool
<?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));
}
?>
/* 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)
*/
<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
echo "The string $testcase consists of all digits.
";
} else {
echo "The string $testcase does not consist of all digits.
";
}
}
?>