is_finite

Checks whether a float is a finite number, returning false for INF, -INF and NAN.

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

Parameters

ParameterTypeDescription
$numfloatThe float value to check.

Return value

booltrue if num is a finite float, false if it is INF, -INF or NAN.

is_finite() answers the one question that matters after a chain of float arithmetic: is this still an ordinary number? It returns false for INF, -INF and NAN, and true for everything else — including 0.0, negative values, and floats right up against PHP_FLOAT_MAX.

When to reach for it

Use it as a gate before anything that will not tolerate a non-finite float: writing to a database column, encoding to JSON, formatting for display, or feeding the value into further arithmetic where a single NAN poisons every downstream result. Averages of empty sets, exponentials that overflow, ratios that hit zero and parsers handed unexpected input are the usual sources.

One check instead of two

It is exactly equivalent to !is_infinite($num) && !is_nan($num), but it states the intent and cannot be written the wrong way round. Reach for the narrower is_infinite() or is_nan() only when you need to react differently to overflow than to an undefined result.

Integer arguments are coerced to float on the way in, so is_finite(5) is true; an int can never be non-finite, so the function only has something interesting to say about floats.

Example

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

When to use it

  • Validating a computed average or ratio before storing it in a numeric database column.
  • Replacing non-finite values with null so json_encode() does not fail on the whole payload.
  • Rejecting a parsed user-supplied number that overflowed during conversion.
  • Stopping a simulation loop as soon as a value stops being a real number.

Frequently asked questions

How do I check if a float is a valid number in PHP?

Use is_finite($num). It returns false for INF, -INF and NAN and true for every ordinary float, so it is the single check for whether a computed value is usable.

What is the difference between is_finite and is_numeric?

is_numeric() asks whether a string or value looks like a number before conversion, while is_finite() asks whether a float that already exists is a real number rather than INF or NAN. They answer different questions and are often used one after the other.

Why does json_encode fail on my float?

json_encode() returns false when the data contains INF or NAN, because JSON cannot represent them. Filter with is_finite() and replace non-finite values with null before encoding.

More examples

Guarding json_encode()

fdiv(1, 0) is INF and fdiv(0, 0) is NAN. JSON has no syntax for either, so json_encode() gives up on the entire array and returns false — not just for the bad keys. Mapping non-finite values to null first produces valid JSON with the good value intact.

Example · php
<?php
$rates = ['a' => 12.5, 'b' => fdiv(1, 0), 'c' => fdiv(0, 0)];

var_dump(json_encode($rates));

$clean = array_map(fn($v) => is_finite($v) ? $v : null, $rates);
echo json_encode($clean), "\n";

is_finite() is the two other checks combined

The two columns match on every row, which is the definition of finite: neither infinite nor NAN. Writing is_finite() instead of the pair removes the chance of forgetting one of the two negations.

Example · php
<?php
foreach ([1.0, INF, -INF, NAN] as $v) {
    $manual = !is_infinite($v) && !is_nan($v);
    printf("%-5s  is_finite=%-5s  manual=%-5s\n",
        var_export($v, true),
        var_export(is_finite($v), true),
        var_export($manual, true)
    );
}

Stopping a runaway calculation

Repeated squaring doubles the exponent each pass, so the value blows past PHP_FLOAT_MAX after a handful of steps and becomes INF. Checking is_finite() inside the loop catches the exact iteration where the result stopped being meaningful, instead of returning INF from a function ten frames up.

Example · php
<?php
$value = 2.0;
for ($step = 1; $step <= 12; $step++) {
    $value = $value ** 2;
    if (!is_finite($value)) {
        echo "lost precision at step $step\n";
        break;
    }
}
var_dump($value);

Discussion

  • Be the first to comment on this lesson.