In PHP, when dealing with floating values, sometimes we need to check whether these values are non-number (NaN). To achieve this, PHP provides a very practical function is_nan() . This article will explain in detail how to use the is_nan function for floating numerical verification and provide actual use cases.
NaN (Not-a-Number) is a special floating value that represents a value that cannot be represented as a number. For example, dividing 0 by 0, or an error occurred while using mathematical operations (such as invalid square root calculations) will result in NaN being returned. In PHP, NaN is considered to be a floating value, but it is different from conventional values (such as integers, floating points). You can use the is_nan() function to determine whether a value is NaN.
is_nan() is a built-in function in PHP to check whether the given value is NaN. This function returns a Boolean value: if the passed parameter is NaN, return true , otherwise false .
grammar:
bool is_nan ( mixed $var )
parameter:
$var : The variable to be checked.
Return value:
Returns true if $var is NaN.
Otherwise, return false .
Suppose you are dealing with a series of floating values and need to verify that it contains invalid NaN values. For values containing NaN, it may result in calculation errors or produce unintended results, so it is important to check and process these values in a timely manner.
Suppose we do some mathematical operations that may produce NaN. This result can be easily judged using is_nan() .
<?php
// Suppose we perform an invalid mathematical operation
$result = 0 / 0; // This will return NaN
if (is_nan($result)) {
echo "turn out NaN,Cannot perform valid calculations。";
} else {
echo "Calculation results:$result";
}
?>
In this example, the result calculated by 0 / 0 isNaN, is_nan() will return true , and the output information is "The result is NaN, and effective calculation cannot be performed."
In practical applications, the data entered by the user may need to be verified. If the user enters an invalid value (such as NaN ), you may want to filter or prompt it before subsequent processing.
<?php
// Suppose the user submits a floating value
$user_input = "NaN"; // Suppose the user enters an invalid value
// Convert to floating value
$input_value = (float)$user_input;
// Check if it is NaN
if (is_nan($input_value)) {
echo "The value entered by the user is invalid(NaN)。Please provide valid values。";
} else {
echo "The value entered by the user is valid:$input_value";
}
?>
This example simulates a scenario where a user submits an invalid value. After converting the user input to a floating value, we can use the is_nan() function to determine whether it is NaN and make corresponding prompts.
In some cases, you may need to process a set of data and exclude the NaN values from it. In this case is_nan() is very useful.
<?php
// Simulate a set of floating values
$data = [3.14, 0/0, 2.71, sqrt(-1), 5.5];
// Filter out NaN value
$valid_data = array_filter($data, function($value) {
return !is_nan($value);
});
echo "Valid data:";
print_r($valid_data);
?>
In this example, we have an array containing NaN values. By using the array_filter() function, we can filter out all NaN values and retain only valid numeric values.
is_nan() is a very practical function in PHP, especially suitable for checking whether NaN exists when processing floating values. Through the examples in this article, you should be able to master how to use is_nan() for floating numerical verification in actual development to avoid calculation errors caused by NaN. Hope these examples help with your PHP programming.
Official PHP document: https://www.php.net/manual/zh/function.is-nan.php