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

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理