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

  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 'on');
  4.  
  5. $a = '123a';
  6. $b = 'b456';
  7.  
  8. echo $a+$b;
  9. ?>

以上代码执行后会提示 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警告。

解决方法

对于这种问题,首先应该在代码逻辑查看,为何会出现混合数值,检查哪里出错导致出现混合数值。

对于(+ - * / ** % << >> | & ^) 的运算,我们也可以加入转换类型方法,把错误的数值转换。

  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 'on');
  4.  
  5. $a = '123a';
  6. $b = 'b456';
  7.  
  8. echo intval($a)+intval($b);
  9. ?>
  1. 加入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. g++使用总结

    学习C和C++的同学应该都知道,gcc是一款跨平台的C/C++编译器,可以在Linux/Windows平台下使用,具有十分强大的功能,结构也十分灵活,并且可以通过不同的前端模块来支持各种语言,如Jav ...

  2. cmd启动MySQL服务器发生错误

    Mysql net start mysql启动,提示发生系统错误 5 拒绝访问  原文:https://blog.csdn.net/angel_guoo/article/details/7919037 ...

  3. kvm安装图终端界面及形界面安装系统

    1.图形界面安装: qemu-img create -f qcow2 /kvm/os/vm-01.qcow2 16G mkdir -p /kvm/iso cd /kvm/iso 上传事先下载好的镜像文 ...

  4. 条款14:在资源管理类中心copying行为(Think carefully about copying behavior in resource-manage classes)

    NOTE: 1.复制RAII 对象必须一并赋值它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 2.普遍而常见的RAII class copying 行为是: 抑制c ...

  5. 框架之---Django

    Django是功能最为健全的一个WEB框架,但就因为过于健全,显得过于臃肿.但是Django中最为正要的就是Middleware.ORM和From表单. Django之web本质 Django之初 D ...

  6. running Fluent on Apocrita Cluster

    two files: code.sh, code.jou code.sh #!/bin/bash #$ -cwd #$ -j y #$ -m bea #$ -M k.ai@qmul.ac.uk #$ ...

  7. uC/OSii之任务划分

    满足以下几个指标将会使软件设计比较简洁高效. 满足实时性指标 任务数目合理 简化软件系统 降低资源需求 设备依赖性划分:主要是对于一些输入输出设备进行划分,输入输出设备分为主动型和被动性.主动型有一个 ...

  8. Using TCP keepalive under Linux

    Linux has built-in support for keepalive. You need to enable TCP/IP networking in order to use it. Y ...

  9. C#静态构造函数和非静态构造函数

    // 使用静态构造函数时,需要注意几点 //1. 一个类中,最多只能有一个静态构造函数,不允许静态构造函数的重载: //2. 不能加任何访问修饰符(public/private/internale等) ...

  10. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...