Mathematics7 min readExamples run on PHP 8.5.10

is_infinite()

Learn how PHP 8.4 is_infinite() detects positive and negative infinity in floats, how it differs from is_nan() and is_finite(), and which mistakes to avoid.

Signature

is_infinite(float $num): bool
Available since PHP 4.2.07.48.08.18.28.38.48.5All versions →

Purpose

is_infinite() answers one narrow question: is this float positive infinity (INF) or negative infinity (-INF)? Floating-point arithmetic can produce infinity when a result is too large for the float type to hold, or when a function such as log(0) has an infinite limit. Once infinity enters a calculation it tends to spread, so it is worth detecting early, before you store, display or compare the value.

The function is part of PHP's math group and is available in all current PHP versions, including PHP 8.4.

Signature

Parameters and defaults

  • $num is the float to inspect. There are no other parameters and no default values.
  • In the default coercive typing mode, an int or a numeric string is converted to a float before the check. An integer can never be infinite, so is_infinite(5) is simply false.
  • A non-numeric string such as "abc" cannot be converted and raises a TypeError.
  • Avoid passing null. It is not a meaningful float, and relying on its coercion hides bugs.

Return value and failure cases

The function returns true when $num is INF or -INF, and false for every other float. That includes ordinary numbers, zero, very large but still finite values such as PHP_FLOAT_MAX, and NAN. It does not return an error code and does not emit warnings for valid floats. The only failure case is the TypeError for a value that cannot be treated as a float.

Example 1: detecting overflow

Multiplying a number near the top of the float range by two overflows to infinity. The sign of the infinity follows the sign of the result, and is_infinite() is true for both.

Example
<?php
$inf = 1e308 * 2;

var_dump($inf, is_infinite($inf));

$negative_inf = -$inf;

var_dump($negative_inf, is_infinite($negative_inf));
Output
float(INF)
bool(true)
float(-INF)
bool(true)

Example 2: where infinity comes from

Since PHP 8, the / operator throws DivisionByZeroError when dividing by zero, so it will not give you infinity. The fdiv() function follows IEEE 754 rules and returns INF instead. Mathematical functions can also yield infinity: log(0) is -INF and exp(1000) overflows to INF.

Example
<?php
$checks = [
    'fdiv' => is_infinite(fdiv(1, 0)),
    'log' => is_infinite(log(0)),
    'exp' => is_infinite(exp(1000)),
    'division' => is_infinite(10 / 4),
];

echo json_encode($checks);
Output
{"fdiv":true,"log":true,"exp":true,"division":false}

Example 3: classifying a float

A float can be finite, infinite or not-a-number, and each state needs different handling. Check is_nan() first, then is_infinite(), and treat everything else as a normal number. The sign test with > 0 distinguishes the two infinities.

Example
<?php
function classify(float $n): string
{
    if (is_nan($n)) {
        return 'nan';
    }
    if (is_infinite($n)) {
        return $n > 0 ? 'positive infinity' : 'negative infinity';
    }
    return 'finite';
}

echo json_encode([classify(3.5), classify(INF), classify(-INF), classify(NAN)]);
Output
["finite","positive infinity","negative infinity","nan"]

Edge cases

  • NAN is not infinite. is_infinite(NAN) is false. Use is_nan() to detect it.
  • Large is not infinite. PHP_FLOAT_MAX is the largest finite float, so is_infinite(PHP_FLOAT_MAX) is false. Only values that overflow past it become INF.
  • Integers are never infinite. Integer overflow in PHP converts the result to a float first, and that float may then be finite or infinite depending on the magnitude.
  • Sign is ignored. The function does not tell you which infinity you have. Compare with zero if the sign matters.
  • JSON cannot hold infinity. json_encode() refuses INF, -INF and NAN by default, so check values before encoding them.

Common mistakes

  1. Expecting / to produce infinity. Dividing by zero with / or intdiv() throws an error in PHP 8. Use fdiv() if you want IEEE results, or validate the divisor.
  2. Using it to detect NAN. NAN is a separate state. A guard that checks only is_infinite() will let NAN through.
  3. Comparing with == to INF for both signs. Writing $x == INF only matches positive infinity, so negative infinity slips past. is_infinite() covers both.
  4. Treating !is_infinite() as proof of a usable number. A value that is not infinite may still be NAN. When you need a real number, use is_finite(), which rejects infinity and NAN together.
  5. Passing arbitrary text. Strings that are not numeric cause a TypeError. Validate or cast user input before calling the function.
  • is_finite() returns true for ordinary floats and false for INF, -INF and NAN.
  • is_nan() checks for not-a-number values specifically.
  • fdiv() performs float division that follows IEEE 754 instead of throwing on a zero divisor.
  • The constants INF, NAN and PHP_FLOAT_MAX are useful for tests and comparisons.

Recap

is_infinite(float $num): bool returns true only for INF and -INF. It returns false for finite numbers, large finite numbers and NAN. Use it together with is_nan() or replace both with is_finite() when you only care whether a result is a usable number.

Tested examples

Detecting overflow to positive and negative infinity
<?php
$inf = 1e308 * 2;

var_dump($inf, is_infinite($inf));

$negative_inf = -$inf;

var_dump($negative_inf, is_infinite($negative_inf));
Output
float(INF)
bool(true)
float(-INF)
bool(true)
Operations that produce infinity
<?php
$checks = [
    'fdiv' => is_infinite(fdiv(1, 0)),
    'log' => is_infinite(log(0)),
    'exp' => is_infinite(exp(1000)),
    'division' => is_infinite(10 / 4),
];

echo json_encode($checks);
Output
{"fdiv":true,"log":true,"exp":true,"division":false}
Classifying finite, infinite and NAN values
<?php
function classify(float $n): string
{
    if (is_nan($n)) {
        return 'nan';
    }
    if (is_infinite($n)) {
        return $n > 0 ? 'positive infinity' : 'negative infinity';
    }
    return 'finite';
}

echo json_encode([classify(3.5), classify(INF), classify(-INF), classify(NAN)]);
Output
["finite","positive infinity","negative infinity","nan"]