Signature
is_infinite(float $num): boolPurpose
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
$numis the float to inspect. There are no other parameters and no default values.- In the default coercive typing mode, an
intor a numeric string is converted to a float before the check. An integer can never be infinite, sois_infinite(5)is simplyfalse. - A non-numeric string such as
"abc"cannot be converted and raises aTypeError. - 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.
$inf = 1e308 * 2;
var_dump($inf, is_infinite($inf));
$negative_inf = -$inf;
var_dump($negative_inf, is_infinite($negative_inf));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.
$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);{"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.
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)]);["finite","positive infinity","negative infinity","nan"]
Edge cases
- NAN is not infinite.
is_infinite(NAN)isfalse. Useis_nan()to detect it. - Large is not infinite.
PHP_FLOAT_MAXis the largest finite float, sois_infinite(PHP_FLOAT_MAX)isfalse. Only values that overflow past it becomeINF. - 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()refusesINF,-INFandNANby default, so check values before encoding them.
Common mistakes
- Expecting
/to produce infinity. Dividing by zero with/orintdiv()throws an error in PHP 8. Usefdiv()if you want IEEE results, or validate the divisor. - Using it to detect NAN.
NANis a separate state. A guard that checks onlyis_infinite()will letNANthrough. - Comparing with
==toINFfor both signs. Writing$x == INFonly matches positive infinity, so negative infinity slips past.is_infinite()covers both. - Treating
!is_infinite()as proof of a usable number. A value that is not infinite may still beNAN. When you need a real number, useis_finite(), which rejects infinity andNANtogether. - Passing arbitrary text. Strings that are not numeric cause a
TypeError. Validate or cast user input before calling the function.
Related functions
is_finite()returnstruefor ordinary floats andfalseforINF,-INFandNAN.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,NANandPHP_FLOAT_MAXare 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
$inf = 1e308 * 2;
var_dump($inf, is_infinite($inf));
$negative_inf = -$inf;
var_dump($negative_inf, is_infinite($negative_inf));
float(INF) bool(true) float(-INF) bool(true)
$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);
{"fdiv":true,"log":true,"exp":true,"division":false}
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)]);
["finite","positive infinity","negative infinity","nan"]