doufu6196 2017-12-06 16:04
浏览 27
已采纳

来自is_numeric / foreach例程的意外输出

Ref: http://php.net/manual/en/function.is-numeric.php.

Example #1 is_numeric().

Why does the Example #1 routine output 1337 4 times instead of 1 time? My expectation is the routine would ouput the other 3 numeric values that follow 1337. Even if I re-arrange the order of the elements it still outputs 1337 4 times. I understand hex and binary values are not allowed, but why does the routine output 1337 for those values. Also, if I change 1337 to 01337 the ouput is 735.

Why these confusing outputs?

<?php
$tests = array(
    "42", 1337, 0x539, 02471, 0b10100111001, 1238.443e2, "not numeric",
    array(), 9.1, null
);

foreach ($tests as $element) {
    if (is_numeric($element)) {
        echo var_export($element, true) . " is numeric", PHP_EOL;
    } else {
        echo var_export($element, true) . " is NOT numeric", PHP_EOL;
    }
}
?>

Output:
'42' is numeric
1337 is numeric
1337 is numeric
1337 is numeric
1337 is numeric
123844.300000000002910383045673370361328125 is numeric
'not numeric' is NOT numeric
array () is NOT numeric
9.0999999999999996447286321199499070644378662109375 is numeric    
NULL is NOT numeric
  • 写回答

2条回答 默认 最新

  • dongtong7990 2017-12-06 16:15
    关注

    Base-N notation syntax

    which you had to know to pull this off

    Hexadecimal

    A number prefixed with 0x will be interpreted as base-16

    Octtal

    A number prefixed with 0 will be interpreted as base-8

    Binary

    A number prefixed with 0b will be interpreted as base-2

    Conversion to Decimal

    • 1337 base10 (base10 value: 1337)
    • 0x539 base16 (base10 value: 1337)
    • 02471 base8 (base10 value: 1337)
    • 0b10100111001 base2 (base10 value: 1337)

    Numbers in strings are always shown in base-10

    Converting these numbers to a string, as with var_export, will convert them to base-10, unless you use number_format.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA