PHP手册:Is_Numeric中的数字转换示例1?

时间:2016-09-12 16:31:23

标签: php evaluation isnumeric

我在PHP文档中遇到了这个例子:

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

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

输出:

'42' is numeric
'1337' is numeric
'1337' is numeric
'1337' is numeric
'1337' is numeric
'1337' is numeric
'not numeric' is NOT numeric
'Array' is NOT numeric
'9.1' is numeric

'42'之后的五个例子都评价为'1337'。我能理解为什么这是'1337e0'(科学记谱法)的情况,但我不明白为什么其他人就是这种情况。

我无法在文档的评论中找到任何提及它的人,我在这里没有找到它,所以任何人都可以解释为什么'0x539','02471'和'0b10100111001'都评估为' 1337' 。

2 个答案:

答案 0 :(得分:3)

输出所有数字时转换为正常表示。这是十进制数字系统和非科学记数法(例如1e10 - 科学浮点数。)

十六进制:

十六进制数字以0x开头,后跟任意0-9a-f

0x539 = 9*16^0 + 3*16^1 + 5*16^2 = 1337

八路:

八进制数以0开头,仅包含整数0-7。

02471 = 1*8^0 + 7*8^1 + 4*8^2 + 2*8^3 = 1337

二进制:

二进制数字开始0b并包含0和/或1 s。

0b10100111001 = 1*2^0 + 1*2^3 + 1*2^4 + 1*2^5 + 1*2^8 + 1*2^10 = 1337

答案 1 :(得分:2)

它们是八进制,十六进制和二进制数。

http://php.net/manual/en/language.types.integer.php