开发者

simple problem in php

<?php 

$n1=$_POST['mont'];
echo $n1;
if($n1==07)
    echo "numeric";
if($n1=="07")
    echo "string开发者_如何学运维";
if($n1==08)
    echo "numeric";

if($n1=="08")
    echo "string";
?>

in this if $_POST["mont"] is 07 the output is both string and integer but if $_POST["mont"] is 08 the output is only string

what may be the cause


Two causes:

  1. PHP is weakly-typed, which means that there's no difference between a string containing a number, and just the number. "5" == 5

  2. Numbers that start with a 0 are considered to be octal. "08" is not a valid octal number, so it can only be considered a string.


Leading zero's on numbers indicate octal format. Octal only goes up to 7, so 08 wouldn't be valid.


Isn't php's consistency fantastic? The same issue happens in JavaScript too. The string with a leading zero is casted as an octal (base-8) number, so "010" would have equaled 8, but not "08".


It's because any number preceding with a '0' in PHP is read as an octal number, so '07' really means 7 in base 8, not base 10 (although they are the same amount). Therefore '08' is considered invalid as an octal and not read as a number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜