W3docs

is_nan()

Today, we will discuss the is_nan() function in PHP. This function is used to determine whether a value is not a number (NaN).

The is_nan() function in PHP reports whether a floating-point value is NaN ("Not a Number") — the special value the IEEE 754 standard uses for results that are mathematically undefined. This page explains what NaN is, why you can't test for it with ==, how is_nan() solves that problem, and the gotchas to watch for.

Syntax

is_nan(float $num): bool

It takes a single floating-point argument and returns true if that value is NaN, and false for every ordinary number (including 0.0 and infinity).

What is NaN?

NaN is a placeholder produced by floating-point math when an operation has no real, representable answer. Common sources include:

  • acos(1.5) — the inverse cosine is only defined for inputs between -1 and 1.
  • sqrt(-1) — there is no real square root of a negative number.
  • log(-1) — the natural logarithm is undefined for non-positive numbers.
  • fdiv(0, 0) — dividing zero by zero floating-point style.
  • The built-in constant NAN itself.

NaN has type double (PHP's float type), so it slips through code that only checks is_float(). That is exactly why a dedicated test exists.

Note: writing 0 / 0 directly throws a DivisionByZeroError in PHP 8+, so it does not silently return NaN. Use fdiv(0, 0) when you want the floating-point NaN result instead of an exception.

Why you can't compare NaN with ==

The single most important property of NaN is that it is not equal to anything, not even itself. Any comparison involving NaN returns false:

<?php
var_dump(NAN == NAN);   // bool(false)
var_dump(NAN === NAN);  // bool(false)
var_dump(NAN < 1);      // bool(false)
var_dump(NAN > 1);      // bool(false)
?>

Because $x == NAN is always false, you cannot detect NaN by comparison. is_nan() is the correct — and only reliable — way to check.

How to Use the is_nan() Function

You pass a value (usually the result of some calculation) to is_nan() and branch on the boolean it returns:

php— editable, runs on the server

Here acos(1.5) is undefined, so it returns NaN, is_nan() returns true, and the script prints The number is not a number.

Validating a calculation result

A typical real-world use is guarding output: run a computation, then refuse to display or store the result if it came out as NaN.

<?php
function safeRatio(float $a, float $b): string {
    $result = $a * sqrt($b); // sqrt of a negative number yields NaN

    if (is_nan($result)) {
        return "Invalid input: result is not a number";
    }

    return "Result: " . $result;
}

echo safeRatio(2, 9), PHP_EOL;   // Result: 6
echo safeRatio(2, -9), PHP_EOL;  // Invalid input: result is not a number
?>
Valueis_nan()is_finite()is_infinite()
5.0falsetruefalse
acos(1.5) (NaN)truefalsefalse
INFfalsefalsetrue

Use is_finite() to confirm a number is an ordinary, usable value, is_infinite() to detect overflow to infinity, and is_nan() specifically for the "undefined result" case. For validating user-supplied strings before they ever reach a calculation, reach for is_numeric() instead.

Conclusion

is_nan() is the dependable way to detect the IEEE 754 NaN value, which equality operators can never catch because NaN is not equal to itself. Use it to validate the output of math functions like acos(), sqrt(), and log() before you trust the result, and pair it with is_finite() and is_float() when you need a complete picture of a float's state.

Practice

Practice
What does the is_nan() function do in PHP?
What does the is_nan() function do in PHP?
Was this page helpful?