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
trueif$numisINFor-INF, andfalsefor any other value, including regular finite numbers andNAN(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 * 10becomesINF. - 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
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.
is_infinite() vs. related functions
PHP groups several float-inspection helpers together. Pick the one that matches the question you're asking:
is_infinite()— is the valueINFor-INF?is_finite()— is the value a normal, finite number (notINF,-INF, orNAN)?is_nan()— is the valueNAN?is_float()— is the variable of type float at all?
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.