Explaining the cause and solution of PHP warning:a Non-numeric value encountered problem

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.