tags:

views:

51

answers:

1
// today is 03 Jan 2009
$datemonth = (int) date("md");

if($datemonth == 0103){
    echo "Match";
} else {
    echo "Not a match";
}

I am receiving Not a match as result. Isn't 0103 equal to 103 when compared as integer? In this situation I can use if($datemonth == 103) for the intended behaviour. But why the logic is failing? A leading zero does not have any value in an integer, right?

+9  A: 

When you begin a numeric literal with a leading zero, it means the number is in octal (base 8). You probably meant it to be a decimal (base 10) number. 0103 in octal is equal to 67 in decimal. Drop the leading zero and your code should work. See PHP documentation for more details on numeric literals.

Asaph
+1 Heh this always gets me. Good catch.
cletus
+1 Damn octals!
missingfaktor
Thanks! That got me definitely!
Nirmal
Oh Thompson and Ritchie! How your invention of octal literals in B has hurt us all! Will we never be free of the scourge of unintentional octals? (Probably not, no. Python has finally had the guts to get shot of them, but it took us 18 years...)
bobince