+

PHP Is_finite (), is_infinite (), is_nan () functions

So this is a safe way to check if a given input is finite or not. is_finite() Syntax:
bool is_finite ($value)
Parameters : the function takes one parameter, which is floating point to be checked.Return type : This function returns TRUE if the given value is finite, FALSE otherwise.Examples:
Input: $value = M_PI_4; Output: TRUE Input: $value = log (0); Output: FALSE
is_infinite() Syntax:
bool is_infinite ($value)
Parameters : the function takes one parameter, which is a floating point that must be checked.Return type : This function returns TRUE if the given value is infinite, FALSE otherwise.Examples:
Input: $value = M_PI_4; Output: FALSE Input: $value = log (0); Output: TRUE
is_nan() function Syntax:
bool is_nan ($value)
Parameters : the function takes one parameter, which is a floating point that must be checked. Return type : This function returns TRUE if the given value is not a number, FALSE otherwise.Examples:
Input: $value = M_PI_4; Output: FALSE Input: $value = acos (1.1); // cos function can not be greater than 1 Output: TRUE
The program below illustrates the operation of the is_finite(), is_infinite(), is_nan() functions in PHP:  
// PHP -code to illustrate the work
// Functions is_finite(), is_infinte() and is_nan() 
// Final value: PI $val1 = M_PI; // Embedded INFINITY value $val2 = INF; // Creates NaN because the COS value can be found
// from -1 up to +1 both inclusive $val3 = acos (- 1.01); echo var_dump ( is_finite ( $val1 ), is_finite ( $val2 ), is_finite ( $val3 )). " " ; echo var_dump ( is_infinite ( $val1 ), is_infinite ( $val2 ), is_infinite ( $val3 )). "" ; echo var_dump ( is_nan ( $val1 ), is_nan ( $val2 ), is_nan ( $val3 )). "" ;  
?>
Output:
bool (true ) bool (false) bool (false) bool (false) bool (true) bool (false) bool (false) bool (false) bool (true)
to watch out for :
  • This function can also check if an expression produces an end result, but in the case of expressions that result in NaN, PHP itself shows an error and returns FALSE by default , for example, when checking the Divided by Zero expression.
  • The is_finite() function is used in many projects to make it safer and more efficient.
  • These methods give very accurate results, but not very time efficient.
Get Solution for free from DataCamp guru