//is_numeric — Finds whether a variable is a number or a numeric string
is_numeric ( $variable ); // returns true or false
if(is_numeric($what_am_i)){
//true
}
// Check if variable is int
$id = "1";
if(!intval($id)){
throw new Exception("Not Int", 404);
}
else{
// this variable is int
}
<?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.
";
}
}
?>