本文介绍php出现Warning: A non-numeric value encountered问题,用实例分析出现这种错误的原因,并提供避免及解决问题的方法。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on'); $a = '123a';
$b = 'b456'; echo $a+$b;
?>

以上代码执行后会提示 Warning: 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.

在使用(+ - * / ** % << >> | & ^) 运算时,例如a+b,如果a是开始一个数字值,但包含非数字字符(123a),b不是数字值开始时(b456),就会有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问题的原因及解决方法的更多相关文章

  1. php 出现Warning: A non-numeric value encountered问题的原因及解决方法

    在使用(+ - * / ** % << >> | & ^) 运算时,例如a+b,如果a是开始一个数字值,但包含非数字字符(123a),b不是数字值开始时(b456),就 ...

  2. 一些常见warning的原因和解决方法

    在入职三周后,终于赶齐了接手项目落下两个月的项目,有了一些自己的空闲时间对项目进行整理.主要整理包括类目的整合,从原来一个系统文件夹下几百个文件整改为以MVC设计思想为原则的分文件夹整理类目,井然有序 ...

  3. Android开发中遇到的问题(四)——Android中WARNING: Application does not specify an API level requirement!的解决方法

    今天在手机上调试运行Andorid项目时,发现Console打印出"WARNING: Application does not specify an API level requiremen ...

  4. 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法

    今天发现电脑的系统时间不对,因此将时钟进行了改动,回头编译Linux kernel的时候,提演示样例如以下的warning: warning:  Clock skew detected.  Your ...

  5. 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法【转】

    本文转载自:http://blog.csdn.net/jeesenzhang/article/details/40300127 今天发现电脑的系统时间不正确,因此将时钟进行了修改,回头编译Linux ...

  6. javascript中出现identifier starts immediately after numeric literal错误原因以及解决方法

    javascript遇到参数是字符型和数字型组合的参数就会出现这种错误,例如alert(1);可以正確輸出alert(str);就會報錯alert("str");可以正確輸出.

  7. move_base Warning: Invalid argument "/map" passed to canTransform argument target_frame的解决方法

    把global_costmap_params.yaml和local_costmap_params.yaml文件里的头几行去掉“/”,然后重新编译就可以了. 效果如下:

  8. “Error: Encountered an improper argument”的解决方法

    之前遇到过的问题,后来解决后再次遇到又忘记了, 这是keil 的bug 路径只要都是字母就可以了

  9. warning: a non-numeric value encountered in line *的解决方法

    今天ytkah在调试项目的时候出现了一个警告warning: a non-numeric value encountered in line 694,查看php官方文档,上面解释说在使用(+ - * ...

随机推荐

  1. Supreme Number

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  2. 8. Truncate undo表空间

    8. Truncate undo表空间 要Truncate Undo 表空间,必须为MySQL实例配置至少两个undo表空间(两个undo表空间可确保一个undo表空间保持活动状态,另一个处于脱机状态 ...

  3. Linux文本检索命令grep笔记

    grep是在linux系统中基于行文本非常实用检索工具,通过该命令可以将匹配到的结果信息输出到终端控制台. 语法格式:grep [-ivnc] '需要匹配的内容' 文件名 常用参数说明: -i 检索的 ...

  4. 杭电 1260 Tickets

    Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this ...

  5. luogu2596 [ZJOI2006]书架

    treap.树是以"优先级"(优先级越小,在书架上越靠上)形成的,堆是以rand()的权值形成的.还要再维护一个原编号. 置顶/置底:找到那个元素,把它拉出来修改优先级再塞回去. ...

  6. Couchbase III(Python Library)

    Couchbase III(Python Library) 第一步 安装 使用pip安装: >pip install couchbase --quiet 确认是否安装成功: >python ...

  7. BNU 4346 Scout YYF I

    A. Scout YYF I Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...

  8. php引入PHPMailer发送邮件

    昨天做了一个发送邮件的功能,如果直接用mail()函数,需要拥有自己的邮件服务器,所有引入PHPMailer类方便快捷,简单写一下开发步骤: 一.拥有自己的邮箱账号(作为发件人邮箱) 分两种情况: 1 ...

  9. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  10. VIM键盘图