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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab