PHP is_infinite() function


The is_infinite() function in PHP is used to determine whether a given value is an infinite number. It returns true if the value is infinite and false if the value is finite or NaN (Not a Number).

Syntax:

is_infinite($value)

Parameters:

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

Return Value:

  • true if the number is infinite (either positive or negative infinity).
  • false if the number is finite or NaN.

Example 1: Checking Infinite Values

<?php // Infinite numbers var_dump(is_infinite(1/0)); // true (division by zero results in positive infinity) var_dump(is_infinite(-1/0)); // true (division by a negative number and zero results in negative infinity) ?>

Output:

bool(true) bool(true)

Example 2: Checking Finite Values

<?php // Finite numbers var_dump(is_infinite(100)); // false var_dump(is_infinite(0)); // false var_dump(is_infinite(-25.5)); // false ?>

Output:

bool(false) bool(false) bool(false)

Example 3: Checking NaN (Not a Number)

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

Output:

bool(false)

Practical Usage:

  • is_infinite() can be useful when you're performing mathematical operations that could result in extremely large numbers or involve division by zero. It helps detect infinite values and avoid potential issues in calculations.

Summary:

  • is_infinite() checks if a value is infinite (positive or negative).
  • It returns true for infinite values and false for finite numbers or NaN.