Skip to content
Advertisement

PHP is_nan() not check for a number

It appears that the PHP is_nan() function isn’t returning anything (not returning true or false). Here is the code it isn’t returning anything in:

$id = 111;
$booltest = is_nan($id);
if ($booltest) {
echo 'Invalid ID: "' . $id . '"';
exit();
}

It won’t fire off the if statement either. I’m using PHP 5. Does anyone know a workaround or way to fix this? Thanks.

Advertisement

Answer

is_nan() does not check if the data type of the variable is a number or not. There is something called not a number or NaN which tells us that the number is not in a representable form. is_nan() function checks if the number is NaN or not.

If you want to check for integers, you can use is_int().

Like below:

$id = 111;
$booltest = is_int($id);
if ($booltest) {
echo 'Invalid ID: "' . $id . '"';
exit();
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement