Home >Backend Development >PHP Tutorial >Why Am I Getting a 'Warning: A Non-Numeric Value Encountered' Error in PHP 7.1?

Why Am I Getting a 'Warning: A Non-Numeric Value Encountered' Error in PHP 7.1?

Linda Hamilton
Linda HamiltonOriginal
2024-12-15 01:25:10252browse

Why Am I Getting a

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn