tags:

views:

46

answers:

4
<?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

+1  A: 

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

Richard Levasseur
A: 

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.

JustChris
+1  A: 

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.

Ignacio Vazquez-Abrams
Actually PHP equates ("08" == 010) so it can still be considered a number
mynameiscoffey
Oh lovely, as if PHP didn't have *enough* hateful little misfeatures already...
Ignacio Vazquez-Abrams
+1  A: 

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".

mynameiscoffey