内容简介:php提示Warning: A non-numeric value encountered的解决方法
在使用(+ - * / ** % << >> | & ^) 运算时,例如a+b,如果a是开始一个数字值,但包含非数字字符(123a),b不是数字值开始时(b456),就会有A non-numeric value encountered警告。
PHP7.1官方文档,对这种错误的解释
New E_WARNING and E_NOTICE errors have been introduced when invalid strings are coerced using operators expecting numbers (+ - * / ** % << >> | & ^) or their assignment equivalents. An E_NOTICE is emitted when the string begins with a numeric value but contains trailing non-numeric characters, and an E_WARNING is emitted when the string does not contain a numeric value.
实例分析
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$a = '123a';
$b = 'b456';
echo $a+$b;
?>
以上代码执行后会提示 Warning: A non-numeric value encountered
解决方法
对于这种问题,首先应该在代码逻辑查看,为何会出现混合数值,检查哪里出错导致出现混合数值。对于(+ - * / ** % << >> | & ^) 的运算,我们也可以加入转换类型方法,把错误的数值转换。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$a = '123a';
$b = 'b456';
echo intval($a)+intval($b);
?>
加入intval方法进行强制转为数值型后,可以解决警告提示问题。
以上所述就是小编给大家介绍的《php提示Warning: A non-numeric value encountered的解决方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- elasticsearch启动时提示内存不足错误的解决方法
- 阿里云短信提示网站发现后门(webshell)文件的解决过程分享
- go to definition 总是提示 rebuild fix 解决办法
- 解决MySQL 8.0 输入无误仍然提示Access denied问题
- Nginx提示Error: Too many open files的解决办法
- CentOS更新软件包提示cannot allocate memory错误与解决
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
精通 CSS(第3版)
[英]安迪•巴德 - Andy Budd、[瑞典]埃米尔•比约克隆德 - Emil Björklund / 李松峰 / 人民邮电出版社 / 2019-2 / 99
本书是CSS设计经典图书升级版,结合CSS近年来的发展,尤其是CSS3和HTML5的特性,对内容进行了全面改写。本书介绍了涉及字体、网页布局、响应式Web设计、表单、动画等方面的实用技巧,并讨论了如何实现稳健、灵活、无障碍访问的Web设计,以及在技术层面如何实现跨浏览器方案和后备方案。本书还介绍了一些鲜为人知的高级技巧,让你的Web设计脱颖而出。一起来看看 《精通 CSS(第3版)》 这本书的介绍吧!