is_infinite

Checks whether a float is positive or negative infinity.

Signature
is_infinite(float $num): bool
  • Added in PHP 4.2.0

Parameters

ParameterTypeDescription
$numfloatThe float value to check.

Return value

booltrue if num is INF or -INF, false otherwise.

is_infinite() returns true when a float is either INF or -INF. It does not tell you which one, and it does not consider NAN infinite.

Where infinity comes from

Float overflow such as 1e308 * 2, fdiv($x, 0) with a non-zero numerator, log(0) which yields -INF, and exp() of a large argument all produce infinities. Ordinary / by zero does not — since PHP 8 that throws a DivisionByZeroError instead, which is why fdiv() exists for code that genuinely wants IEEE semantics.

Checking the sign

Unlike NAN, infinities compare normally, so once is_infinite($x) is true you can test $x > 0 to separate the two, or compare directly with $x === INF. That makes it easy to distinguish "overflowed upward" from "overflowed downward" when clamping.

What it misses

is_infinite(NAN) is false. If you are validating a computed value, is_finite() is almost always the check you actually want. Reach for is_infinite() when overflow and undefined results deserve different treatment — for example clamping an infinity to PHP_FLOAT_MAX while rejecting a NAN outright.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Detecting that an accumulating product or exponential has overflowed mid-loop.
  • Clamping an overflowed result to PHP_FLOAT_MAX instead of failing the request.
  • Distinguishing a positive from a negative overflow when formatting a value for display.
  • Deciding whether fdiv() hit a zero denominator without wrapping the call in a try block.

Frequently asked questions

How do I check for infinity in PHP?

is_infinite($num) returns true for both INF and -INF. To tell them apart, test the sign afterwards with $num > 0, since infinities compare normally.

Does is_infinite return true for NAN?

No. NAN is not infinite, so is_infinite(NAN) is false. Use is_finite() when you want a single check that rejects INF, -INF and NAN together.

What produces INF in PHP?

Float overflow such as 1e308 * 2, fdiv($x, 0) with a non-zero numerator, log(0) which gives -INF, and exp() of a large argument. Plain division by zero throws DivisionByZeroError rather than returning INF.

More examples

Telling the two infinities apart

Infinities are ordered, so a plain > 0 test after is_infinite() reliably separates INF from -INF. The fourth value, fdiv(0, 0), is NAN rather than an infinity and falls through to the is_nan() branch — a reminder that is_infinite() alone does not catch every bad float.

Example · php
<?php
$values = [fdiv(1, 0), fdiv(-1, 0), 42.0, fdiv(0, 0)];

foreach ($values as $v) {
    if (is_infinite($v)) {
        echo $v > 0 ? "positive infinity\n" : "negative infinity\n";
    } elseif (is_nan($v)) {
        echo "undefined (NAN)\n";
    } else {
        echo "finite: $v\n";
    }
}

Catching an overflow inside a loop

The first multiplication is fine at 1e200, but the second pushes the product to 1e400, which no double can hold, so it becomes INF and the loop stops. Without the check the function would return INF and every later calculation built on it would be meaningless.

Example · php
<?php
$product = 1.0;
foreach ([1.0e200, 1.0e200, 2.0] as $factor) {
    $product *= $factor;
    if (is_infinite($product)) {
        echo "overflowed after multiplying by $factor\n";
        break;
    }
}
var_dump($product);

Discussion

  • Be the first to comment on this lesson.