Google News
logo
PHP Program to Check if a numeric value is finite or infinite
In PHP, you can use the `is_finite()` and `is_infinite()` functions to check if a numeric value is finite or infinite.

The `is_finite()` function returns `true` if the given value is a finite number (i.e., not infinite or not a number), and `false` otherwise.

Here's an example :
Program :
<?php
$num = 123.45;
if (is_finite($num)) {
    echo "$num is a finite number";
} else {
    echo "$num is not a finite number";
}
?>
Output :
123.45 is a finite number
In this example, the variable `$num` is assigned the value `123.45`. The `if` statement checks if `$num` is finite using the `is_finite()` function.

If `$num` is a finite number, the message "123.45 is a finite number" will be printed.

The `is_infinite()` function, on the other hand, returns `true` if the given value is infinite, and `false` otherwise.

Here's an example :
Program :
<?php
$num = INF;
if (is_infinite($num)) {
    echo "$num is infinite";
} else {
    echo "$num is not infinite";
}
?>
Output :
INF is infinite
In this example, the variable `$num` is assigned the value `INF`, which represents infinity. The `if` statement checks if `$num` is infinite using the `is_infinite()` function. If `$num` is infinite, the message "INF is infinite" will be printed.

If the value is not a number (NaN), both `is_finite()` and `is_infinite()` functions will return `false`.