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
Release: 2024-12-15 01:25:10
Original
252 people have browsed it

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']);
Copy after login

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";
Copy after login

To concatenate strings correctly, use the '.' operator as follows:

$greeting = "Hello" . "World";
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template