This paper introduces the problem of PHP warning:a non-numeric value encountered , analyzes the cause of this error with an example, and provides a method to avoid and solve the problem.
<?phperror_reporting (E_all); Ini_set (' display_errors ', ' on '); $a = ' 123a '; $b = ' b456 '; echo $a + $b;? >
The above code will prompt warning:a non-numeric value encountered after execution
View PHP7.1 official documentation explaining this error
New e_warning and E_notice errors has been introduced when invalid strings is coerced using operators expecting numbers (+-*/* *% << >> | & ^) or their assignment equivalents. An e_notice was emitted when the string begins with a numeric value but contains trailing non-numeric characters, and an e_ WARNING is emitted if the string does not contain a numeric value.
When using (+-*/* *% << >> | & ^) operations, such as A+b, if A is the start of a numeric value but contains a non-numeric character (123a), B is not a numeric value at the beginning (b456), there will be a non-numeric value Encountered warning.
Workaround
For this kind of problem, you should first look at the code logic, why mixed values appear, and check where errors result in mixed values.
For operations (+-*/*% << >> | & ^) , we can also add the conversion type method to convert the wrong value.
<?phperror_reporting (E_all); Ini_set (' display_errors ', ' on '); $a = ' 123a '; $b = ' b456 '; echo intval ($a) +intval ($b);? >
After you have joined the intval method to force a numeric type, you can resolve the warning question.
This article explains PHP warning:a Non-numeric value encountered problem causes and solutions, more relevant content please focus on the PHP Chinese web.