When processing floating point numbers in PHP, is_infinite() is a function used to determine whether a variable is infinite. Although it is semantics themselves, due to the computational properties of floating-point numbers and the loose type system of PHP, if used improperly, some difficult-to-observe problems will arise. This article will explore common pitfalls when judging floating-point values using is_infinite() and provide some practical suggestions to avoid these problems.
Infinite values usually appear in the following situations:
Illegal zero-dividing operation (such as 1.0 / 0.0 )
Overflow caused by exponential explosion (such as exp(10000) )
Imported data in other languages or systems itself has infinite values (such as JSON, API returns)
These cases will produce INF or -INF values, which are considered infinite in PHP and can be identified by is_infinite() .
$val = 1.0 / 0.0;
if (is_infinite($val)) {
echo "The value is infinitely large";
}
Many developers only call is_infinite() on the final result, but ignore the calculation results of the intermediate steps may be infinite. For example:
$a = 1e308;
$b = 1e308;
$c = $a * $b;
if (!is_infinite($c)) {
echo "Computational security";
} else {
echo "Overflow occurred";
}
In the above code, $a * $b result will be INF , even if $a and $b themselves are legal. Therefore, checks should also be inserted in the critical calculation steps, rather than just looking at the final result.
When processing floating point values parsed from external APIs or JSON, data containing INF or -INF may be encountered. PHP's json_encode() and json_decode() cannot handle these values correctly by default:
$data = ['value' => INF];
$json = json_encode($data);
// return false,because INF It's illegal JSON value
To handle this kind of situation, you can customize the filtering logic:
function safe_json_encode($data) {
array_walk_recursive($data, function (&$item) {
if (is_infinite($item)) {
$item = ($item > 0) ? 'INF' : '-INF';
}
});
return json_encode($data);
}
Or check before reading an external source:
$json = file_get_contents('https://gitbox.net/api/data');
$data = json_decode($json, true);
if (is_infinite($data['value'])) {
// Do error handling
}
When you sort or compare an array with infinite values, you may experience a logical error. For example:
$values = [1.5, INF, 3.0];
sort($values);
Although INF will be correctly ranked last, if the following judgment occurs in the business logic:
if ($values[2] > 1000000) {
// Whether it really means out of range?
}
In this case, you should clearly determine whether it is infinite, rather than compare it to a certain threshold.
Explicit check in key operations: call is_infinite() after all calculations that may lead to extreme values.
External data cleaning: Preprocess all floating point input values, remove INF or replace them with the business-acceptable maximum value.
Encapsulation check function: establish a unified floating-point value verification method, such as:
function is_valid_float($val) {
return is_float($val) && !is_nan($val) && !is_infinite($val);
}
Set business restrictions on the results: For example, limit floating point values such as price and amount to a reasonable range:
if ($amount > 1e12 || is_infinite($amount)) {
throw new Exception("Invalid amount");
}
Using is_infinite() in PHP seems simple, but behind it there are several potential risks such as floating point accuracy, input verification, and boundary processing. Developers should be vigilant when designing floating-point logic and use boundary checking and data verification functions reasonably to effectively avoid unpredictable behavior or security risks during runtime. By correctly identifying and processing floating point overflow, the robustness and stability of the system can be greatly improved.