Current Location: Home> Latest Articles> Example of using is_nan function and is_infinite function

Example of using is_nan function and is_infinite function

gitbox 2025-05-27

In PHP, we often need to process various types of data, including numerical values, strings, boolean values, etc. When we are doing mathematical operations, we may encounter non-numeric values ​​(NaN) or infinitely large values. To safely handle these special values, PHP provides two very useful functions: is_nan() and is_infinite() .

This article will explore how to use these two functions to determine non-numerical and infinite cases in PHP. We will also introduce how to use these two functions to prevent program errors and ensure the stability of the code.

What are NaN and Infinite Large?

In calculations, NaN (Not-a-Number) represents an undefined numerical value, which is usually used to represent illegal results in mathematical operations, such as 0/0 or sqrt(-1) . Infinity represents a value that exceeds the range that can be represented by the computer, such as 1/0 or log(0) .

These two special values ​​usually cause unexpected behavior of the program, especially when performing comparisons or arithmetic operations. Therefore, it is very important to check in advance and handle them correctly.

is_nan() and is_infinite() functions

PHP provides two built-in functions to detect these special cases:

  1. is_nan()
    Used to determine whether a value is NaN . Return true if the given value is NaN , otherwise false .

    Example:

     $value = sqrt(-1); // turn out NaN
    if (is_nan($value)) {
        echo "The value is NaN";
    }
    
  2. is_infinite()
    Used to determine whether a value is (positive infinite or negative infinite). Return true if the given value is positive or negative infinite, otherwise false .

    Example:

     $value = 1 / 0; // turn out Just infinity
    if (is_infinite($value)) {
        echo "The value is无穷大";
    }
    

Use is_nan() and is_infinite() to determine special circumstances

In some complex calculations, we may encounter NaN and Infinity at the same time, so we need to use these two functions in combination to ensure that our code handles these special cases correctly.

Example:

 $value = 1 / 0; // Just infinity

if (is_nan($value)) {
    echo "The value is NaN";
} elseif (is_infinite($value)) {
    echo "The value is无穷大";
} else {
    echo "The value is有效的数值";
}

In this example, we first check whether it is NaN and then check whether it is infinity. This way ensures that we can handle these special circumstances accurately.

Handle complex expressions

Sometimes our calculations can be complex and involve multiple steps. In this case, we can still ensure that the results of each step are valid by is_nan() and is_infinite() .

Example:

 $numerator = 0;
$denominator = 0;
$result = $numerator / $denominator; // turn out NaN

if (is_nan($result)) {
    echo "turn out NaN";
} elseif (is_infinite($result)) {
    echo "turn out无穷大";
} else {
    echo "turn out有效的数值";
}

In this example, we can see that even the results in multiple operation steps can be checked through is_nan() and is_infinite() .

Use scenarios

  1. Prevent illegal mathematical calculations <br> When writing a calculation program, if the user enters illegal data (such as dividing by zero, opening a square root of a negative number, etc.), you can pre-check it through is_nan() and is_infinite() to avoid program crashes or meaningless results.

  2. Data Verification <br> When dealing with numeric values ​​obtained from external sources such as databases or APIs, we may encounter unpredictable infinity or NaN values. These functions can be used to verify and clean the data to ensure the stability of subsequent processing.

summary

Using is_nan() and is_infinite() can effectively judge non-numerical and infinite situations in PHP, helping us avoid the program crashing when encountering these special situations. By judging and processing these special values ​​in advance, we can ensure that our code is more robust and reliable.

Hope this article helps you understand how to use is_nan() and is_infinite() functions in PHP to handle special numeric values. With these simple functions, you can ensure that the program responds correctly when facing illegal or extreme values.