PHP is_nan() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The is_nan() function checks whether a value is NAN, which stands for Not A Number.

NAN appears when a calculation has no valid answer, such as the square root of a negative number or 0 divided by 0.

Syntax

Call is_nan() with the values you need. Start with this line.

PHP
You pass a number to is_nan() and it returns true or false.
CallWhat it does
is_nan(NAN)Returns true.
is_nan(5.5)Returns false.
is_nan(sqrt(-1))Returns true, because the square root of -1 is not a real number.

You cannot check for NAN with ==, because NAN is not equal to anything, not even itself. Always use is_nan().

Library declarations
PHP
is_nan(float $num): bool

Parameters

ParameterDescription
numRequired. The float value to check.

Return Value

Return ValueDescription
booltrue if num is NAN, otherwise false.

PHP Examples

Example 1 — Detect NAN

Check NAN, a normal number and an invalid square root.

PHP
<?php
echo var_export(is_nan(NAN), true) . PHP_EOL;
echo var_export(is_nan(5.5), true) . PHP_EOL;
echo var_export(is_nan(sqrt(-1)), true) . PHP_EOL;

How It Works

  1. NAN is a built-in constant that means not a number, so the result is true.
  2. 5.5 is a valid number, so the result is false.
  3. sqrt(-1) has no real answer, so PHP returns NAN and is_nan() returns true.
  4. var_export(..., true) turns the boolean into printable text.

Example 2 — NAN is not equal to itself

See why == cannot be used to find NAN.

PHP
<?php
echo var_export(NAN == NAN, true) . PHP_EOL;
echo var_export(is_nan(NAN), true) . PHP_EOL;
echo var_export(is_nan(INF), true) . PHP_EOL;
echo var_export(is_nan(INF - INF), true) . PHP_EOL;

How It Works

  1. NAN == NAN is false because NAN is never equal to any value, including itself.
  2. is_nan(NAN) correctly returns true.
  3. INF is infinity, not NAN, so is_nan(INF) is false.
  4. INF - INF has no meaningful answer, so the result is NAN.

Example 3 — Safe square root for user input

Return a friendly message when the input cannot have a square root.

PHP
<?php
function safeSqrt(float $value): string {
    $result = sqrt($value);

    if (is_nan($result)) {
        return "Invalid input";
    }

    return (string) $result;
}

echo safeSqrt(16) . PHP_EOL;
echo safeSqrt(-4) . PHP_EOL;
echo safeSqrt(25) . PHP_EOL;

How It Works

  1. sqrt() returns NAN for negative numbers instead of throwing an error.
  2. is_nan() checks the result and the function returns "Invalid input" if it is NAN.
  3. For 16 and 25 the results 4 and 5 are normal numbers, so they are returned as text.
  4. This pattern keeps your page from showing confusing NAN values.

Frequently Asked Questions

It returns true when a value is NAN (Not A Number) and false otherwise.
NAN is not equal to anything, including itself, so NAN == NAN is false. Use is_nan() instead.
Invalid math like sqrt(-1), fdiv(0, 0), INF - INF or fmod(5, 0).
No. INF is infinity, and is_nan(INF) is false. Use is_infinite() to detect it.
It expects a float. Check with is_numeric() first if your value comes from user input.

Next: log()

log() returns the logarithm of a number. By default it gives the natural logarithm, which .

log() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful