Current Location: Home> Latest Articles> Use the is_nan function to determine whether the floating value is invalid

Use the is_nan function to determine whether the floating value is invalid

gitbox 2025-05-28

In PHP programming, when processing floating values, you often encounter some "invalid" values. For example, some mathematical operations may return NaN (Not a Number). In order to determine whether a floating value is NaN , PHP provides the is_nan function. This article will introduce how to use this function to determine whether a floating value is an invalid value and demonstrate the corresponding code.

1. What is NaN ?

NaN is a special value of the floating numeric type, indicating an "invalid" value, which usually occurs in the following situations:

  • Mathematical operations cannot return a valid numerical value, such as 0 divided by 0, or calculate an invalid square root.

  • Some external data sources pass values ​​that are not numbers, such as received strings that cannot be converted into valid numbers.

In PHP, NaN is not equal to any value, including itself. Therefore, when comparing two NaN values, the result is always false , which makes it impossible for us to directly use the conventional comparison operation to determine whether a value is NaN .

2. The role of is_nan function

PHP provides the is_nan function to detect whether a floating value is NaN . It is very simple to use:

 <?php
$value = 0 / 0; // Generate a NaN value
if (is_nan($value)) {
    echo "This is a NaN value";
} else {
    echo "This is a有效的数value";
}
?>

In the above code, $value will be assigned to NaN (through the 0/0 operation), and then check whether it is NaN through the is_nan() function. If so, output the corresponding prompt information.

3. Return value of is_nan

  • If the variable is NaN , the is_nan function returns true .

  • If the variable is not NaN , false is returned.

4. Sample code: Detect NaN value

Here are some common example codes for NaN value detection:

 <?php
$values = [0 / 0, log(-1), sqrt(-1), 100, 3.14];

foreach ($values as $value) {
    if (is_nan($value)) {
        echo "$value It&#39;s one NaN value\n";
    } else {
        echo "$value no NaN value\n";
    }
}
?>

In the above code, we checked an array with multiple values, some of which are invalid NaN values, such as log(-1) and sqrt(-1) . The program checks each value and outputs whether it is NaN .

5. Use is_nan to determine whether the URL parameter is valid

Sometimes we may get floating values ​​from the URL as parameters and use the is_nan function to determine whether it is valid. Here is an example based on URL parameters:

 <?php
$url = 'http://gitbox.net/somepage.php?value=NaN';
parse_str(parse_url($url, PHP_URL_QUERY), $queryParams);

if (isset($queryParams['value']) && is_nan((float)$queryParams['value'])) {
    echo "URL 中的value无效 (NaN)";
} else {
    echo "URL 中的value有效";
}
?>

In this example, we simulated getting a URL parameter from gitbox.net and determining whether it is NaN . First, parse the query string in the URL, and then use is_nan to verify whether the passed floating value is valid.

6. Things to note

  • is_nan only applies to floating numeric types. Calling this function for other types of data (such as strings, integers, etc.) will return false .

  • If you need to determine whether a variable is a number and is not NaN , you can use the is_numeric function and the is_nan function in combination.

 <?php
$value = "abc";
if (is_numeric($value) && !is_nan((float)$value)) {
    echo "$value It&#39;s one有效的数value";
} else {
    echo "$value no有效的数value";
}
?>

in conclusion

The is_nan function is an easy way to determine whether the floating value is NaN . By using it, developers can accurately detect and process invalid values, ensuring that the code works properly when encountering values ​​that do not meet expectations. Especially when processing external data sources or user input, the rational use of the is_nan function can effectively avoid potential errors.