Home >Backend Development >PHP Tutorial >Why Am I Getting a 'Warning: A Non-Numeric Value Encountered' Error in PHP 7.1?
Warning: A Non-Numeric Value Encountered
Recently, a PHP developer encountered an error upon updating to PHP 7.1. The error message, "Warning: A non-numeric value encountered," indicated an issue in line 29 of the codebase.
Line 29
The code in line 29 is as follows:
$sub_total += ($item['quantity'] * $product['price']);
This line attempts to add the product of $item['quantity'] and $product['price'] to the $sub_total variable. However, the error indicates that a non-numeric value was encountered, preventing the calculation.
Possible Solution
While the specific issue reported in the question may differ from the answer provided, the same error can occur in other instances. One common cause is incorrect concatenation of strings using the ' ' operator instead of the '.' operator.
Example
The following code will trigger the same error:
$greeting = "Hello" + "World";
To concatenate strings correctly, use the '.' operator as follows:
$greeting = "Hello" . "World";
Additional Considerations
It's important to ensure that all values involved in mathematical operations are numeric and compatible with the expected data types. Strings, arrays, or other non-numeric data types will result in errors.
The above is the detailed content of Why Am I Getting a 'Warning: A Non-Numeric Value Encountered' Error in PHP 7.1?. For more information, please follow other related articles on the PHP Chinese website!