Nir Alfasi
Nir Alfasi

Reputation: 53525

php eval - weird behavior

If you had to guess what will be the output of the following php code:

$str = "011";
eval("\$num = $str;");
echo "number is: ".$num;

I bet you would never say "9". Well, neither did I...
I'd love to hear an explanation for this bizarre behavior!

Upvotes: 1

Views: 102

Answers (1)

Gustav Larsson
Gustav Larsson

Reputation: 8487

Starting a digit with a leading 0 indicates base 8, in which 11 is 9.

Still confused? Read this: http://en.wikipedia.org/wiki/Octal

Upvotes: 5

Related Questions