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
which you had to know to pull this off
A number prefixed with 0x
will be interpreted as base-16
A number prefixed with 0
will be interpreted as base-8
A number prefixed with 0b
will be interpreted as base-2
1337
base10 (base10 value: 1337)0x539
base16 (base10 value: 1337)02471
base8 (base10 value: 1337)0b10100111001
base2 (base10 value: 1337)Converting these numbers to a string, as with var_export, will convert them to base-10, unless you use number_format
.
Because that is your values.
For example, if you start a number with a zero, it is means, this number is in octal number system. 02471 = 1337
in decimal number system. The other is the hexa, the remaining the binary.