php提示Warning: A non-numeric value encountered的解决方法

栏目: PHP · 发布时间: 6年前

内容简介: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的解决方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数学之美 (第二版)

数学之美 (第二版)

吴军 / 人民邮电出版社 / 2014-11 / 49.00元

几年前,“数学之美”系列文章原刊载于谷歌黑板报,获得上百万次点击,得到读者高度评价。读者说,读了“数学之美”,才发现大学时学的数学知识,比如马尔可夫链、矩阵计算,甚至余弦函数原来都如此亲切,并且栩栩如生,才发现自然语言和信息处理这么有趣。 在纸本书的创作中,作者吴军博士几乎把所有文章都重写了一遍,为的是把高深的数学原理讲得更加通俗易懂,让非专业读者也能领略数学的魅力。读者通过具体的例子学到的......一起来看看 《数学之美 (第二版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码