dongqindan4406 2017-03-16 17:36
浏览 69
已采纳

php 7.1警告“非数字值”仅适用于数字值“0-4”

I have the following working code:

$nif = 'A12345678';

$even = $nif[2] + $nif[4] + $nif[6];

for ($i = 1; $i < 8; $i += 2)
{
    echo $nif[$i];
    $even += substr((2 * $nif[$i]), 0, 1) + substr((2 * $nif[$i]), 1, 1);
}

Output for PHP 7.0.14

1357

Output for PHP 7.1

1<br />
<b>Warning</b>:  A non-numeric value encountered in <b>[...][...]</b> on line <b>22</b><br />
3<br />
<b>Warning</b>:  A non-numeric value encountered in <b>[...][...]</b> on line <b>22</b><br />
57

Now if I change the value of $nif to:

$nif = 'A52545678';

The output for 7.1 would be:

5557

Already tried passing a normal variable to substr() but that's not the problem:

$a = $nif[$i];
$even += substr((2 * $a), 0, 1) + substr((2 * $a), 1, 1);

Why is PHP 7.1 or substr() from PHP 7.1 treating numeric values in the range '0-4' as non-numeric values?

  • 写回答

1条回答 默认 最新

  • dsdioa9545 2017-03-16 17:54
    关注

    The problem lies in the following part of the code:

    substr((2 * $nif[$i]), 1, 1)
    
    1. for $i = 1;, 2 * $nif[$i] will yield 2.
    2. 2 gets casted to a string by substr() as it tries to extract the second character, which doesn't exist, since the string is '2'
    3. substr() yields an empty string
    4. the empty string '' is passed to the + operator, which produces the A non-numeric value encountered warning, since an empty string is not a valid numeric string.

    The reason it works with 5 and 7 is that in step 2 substr() will operate on a 2-character string instead of 1-character string and will thus yield a valid numeric string.

    This warning is new in PHP 7.1, according to Migrating from PHP 7.0.x to PHP 7.1.x:

    Notices and warnings on arithmetic with invalid strings

    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.

    You can circumvent this with explicit (int) casts before the substr() calls:

    (int) substr((2 * $nif[$i]), 1, 1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测