W3docs

is_finite()

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

The is_finite() function in PHP tells you whether a number is finite — that is, a normal number that is neither infinite (INF / -INF) nor NAN (Not a Number). This page explains what counts as finite, how the function handles non-numeric values, and when you'd reach for it in real code.

Syntax

is_finite(float $num): bool
  • $num — the value to test. It is treated as a float; numeric strings and integers are converted automatically.
  • Return valuetrue if $num is a finite number, false if it is INF, -INF, or NAN.

Why "finite" matters

Floating-point arithmetic can produce special values that are not ordinary numbers:

  • INF and -INF appear when a result overflows the range of a float (for example dividing by zero with floats, or PHP_FLOAT_MAX * 2).
  • NAN appears for undefined operations such as sqrt(-1) or INF - INF.

These values silently propagate through later calculations and break comparisons (NAN == NAN is even false). is_finite() lets you detect them before they corrupt your output.

Basic example

php— editable, runs on the server

This sets a variable to an ordinary number and prints The number is a finite number, because 10 is finite.

How different values behave

The example below shows what is_finite() returns for the common edge cases:

<?php
var_dump(is_finite(10));               // bool(true)  — a normal integer
var_dump(is_finite(3.14));             // bool(true)  — a normal float
var_dump(is_finite("42"));             // bool(true)  — numeric string is cast to float
var_dump(is_finite(PHP_FLOAT_MAX));    // bool(true)  — large but still finite
var_dump(is_finite(INF));              // bool(false) — positive infinity
var_dump(is_finite(-INF));             // bool(false) — negative infinity
var_dump(is_finite(PHP_FLOAT_MAX * 2));// bool(false) — overflows to INF
var_dump(is_finite(NAN));              // bool(false) — Not a Number
?>

Note that is_finite() does not validate that a value is numeric — it casts first. is_finite("hello") becomes is_finite(0.0) and returns true. If you need to confirm a value is actually a number, validate it first with is_numeric().

A practical use case

A common pattern is guarding a calculation that might overflow to INF:

<?php
function safeSquare($x) {
  $result = $x * $x;
  // If the multiplication overflows, $result becomes INF.
  return is_finite($result) ? $result : null;
}

var_dump(safeSquare(3.0));            // float(9)
var_dump(safeSquare(PHP_FLOAT_MAX));  // NULL — squaring overflows to INF
?>

Here safeSquare() returns null instead of letting INF leak into the rest of the program, so the caller can handle the failure cleanly. (Note that in PHP 8+, integer/float division by zero throws DivisionByZeroError rather than producing INF, so overflow is the typical way you encounter an infinite result.)

  • is_infinite() — the opposite check: returns true for INF / -INF.
  • is_nan() — tests specifically for NAN.
  • is_numeric() — checks whether a value is a number or numeric string before you do math.
  • is_float() — checks whether a variable's type is float.

Conclusion

is_finite() is a small but important guard when you work with floating-point math in PHP. It returns true only for ordinary numbers and false for INF, -INF, and NAN, letting you catch overflow and undefined results before they spread. Pair it with is_numeric() for input validation and with is_infinite() / is_nan() when you need to know exactly which special value you hit.

Practice

Practice
What is the correct usage of the 'is_finite' function in PHP?
What is the correct usage of the 'is_finite' function in PHP?
Was this page helpful?