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

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

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

查看所有标签

猜你喜欢:

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

Clojure编程

Clojure编程

Chas Emerick、Brian Carper、Christophe Grand / 徐明明、杨寿勋 / 电子工业出版社 / 2013-3-26 / 99.00元

Clojure是一种实用的通用语言,它是传奇语言LISP的方言,可与Ruby、Python等动态语言相媲美,更以无缝Java库、服务,以及拥有JVM系统得天独厚的资源优势而胜出。本书既可以用来熟悉Clojure基础知识与常见例子,也可了解其相关的实践领域与话题,更可以看到这一JVM平台上的LISP如何帮助消除不必要的复杂性,为大家在编程实践中解决最具挑战性的问题开辟新的选择——更具灵活性,更适于W......一起来看看 《Clojure编程》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码