W3docs

is_infinite()

Today, we will discuss the is_infinite() function in PHP. This function is used to determine whether a value is infinite.

The is_infinite() function in PHP checks whether a floating-point value is infinite — that is, equal to INF (positive infinity) or -INF (negative infinity). This page explains what counts as "infinite," how the function behaves, and the practical situations where you'd reach for it.

Syntax and return value

is_infinite(float $num): bool
  • $num — the value to test.
  • Returns true if $num is INF or -INF, and false for any other value, including regular finite numbers and NAN (Not a Number).

is_infinite() only makes sense for floats. Integers can never be infinite in PHP, so the function always returns false for them.

Where do infinite values come from?

In PHP, floating-point numbers follow the IEEE 754 standard, which has special values for infinity. You get INF or -INF when:

  • You use the predefined constant directly: $x = INF;
  • A calculation overflows the range of a float (roughly ±1.8e308): 1.0e308 * 10 becomes INF.
  • A division by floating-point zero occurs, or a function like log(0) returns negative infinity.

Because these values quietly propagate through later math (INF + 1 is still INF), checking for them is how you catch a calculation that has gone out of range.

Basic usage

php— editable, runs on the server

Here we assign the INF constant to $number, then call is_infinite() to test it. The condition is true, so the script prints The number is infinite.

What is and isn't infinite

The table of values below shows exactly what is_infinite() returns. Note that overflow produces INF, while a large-but-representable number does not:

<?php
var_dump(is_infinite(INF));               // bool(true)
var_dump(is_infinite(-INF));              // bool(true)
var_dump(is_infinite(PHP_FLOAT_MAX * 2)); // bool(true)  — overflow
var_dump(is_infinite(1 / 0.0001));        // bool(false) — large but finite
var_dump(is_infinite(42));                // bool(false) — an integer
var_dump(is_infinite(NAN));               // bool(false) — NAN is not infinite
?>

The key gotcha: NAN (the result of operations like sqrt(-1)) is not infinite, so is_infinite(NAN) is false. Use is_nan() to detect that case, and is_finite() to confirm a value is an ordinary, usable number.

A practical example: guarding against overflow

A common real-world use is validating the result of a calculation before you store or display it. If an operation overflowed to INF, your code can react instead of passing infinity downstream:

<?php
function guardedMultiply(float $a, float $b): string
{
    $product = $a * $b;

    return is_infinite($product)
        ? "Result overflowed to infinity"
        : "Result: $product";
}

echo guardedMultiply(1.0e200, 1.0e200), PHP_EOL; // Result overflowed to infinity
echo guardedMultiply(2, 3), PHP_EOL;             // Result: 6
?>

Multiplying 1.0e200 by itself exceeds the float range, so the product is INF and the guard catches it. The second call returns a normal value.

PHP groups several float-inspection helpers together. Pick the one that matches the question you're asking:

For more numeric helpers, see the PHP Math reference.

Conclusion

is_infinite() returns true only for INF and -INF, letting you detect floating-point overflow and other operations that produce infinity. Pair it with is_finite() and is_nan() to fully validate the result of any float calculation before relying on it.

Practice

Practice
What is the purpose of the 'is_infinite()' function in PHP?
What is the purpose of the 'is_infinite()' function in PHP?
Was this page helpful?