PHP is_finite() function


The is_finite() function in PHP is used to check whether a given value is a finite number. It returns true if the value is finite and false if it is infinite or not a number (NaN).

Syntax:

is_finite($value)

Parameters:

  • $value: The number or expression you want to check.

Return Value:

  • true if the number is finite (i.e., it is not infinite or NaN).
  • false if the number is infinite or NaN.

Example 1: Checking Finite Values

<?php // Finite numbers var_dump(is_finite(100)); // true var_dump(is_finite(-25.5)); // true var_dump(is_finite(0)); // true ?>

Output:

bool(true) bool(true) bool(true)

Example 2: Checking Infinite Values

<?php // Infinite numbers var_dump(is_finite(log(0))); // false (log(0) results in -INF) var_dump(is_finite(1/0)); // false (1 divided by 0 results in INF) ?>

Output:

bool(false) bool(false)

Example 3: Checking NaN (Not a Number)

<?php // NaN (Not a Number) var_dump(is_finite(acos(2))); // false (acos(2) results in NaN) ?>

Output:

bool(false)

Practical Usage:

You can use is_finite() to validate numbers when performing calculations where it's important that the result is a finite number, for instance, in physics simulations, finance calculations, or when working with scientific data.

Summary:

  • is_finite() checks whether a number is finite (not infinite or NaN).
  • It's commonly used to ensure calculations do not result in infinite values or errors caused by undefined mathematical operations.