a single line of code :
echo eval("return 011");
the output is 9
because PHP think 011 is an octal value : Ok.
Now, how to force php to evaluate "011" as "11" ?
a single line of code :
echo eval("return 011");
the output is 9
because PHP think 011 is an octal value : Ok.
Now, how to force php to evaluate "011" as "11" ?
Just use the +
operator to turn a string into a regular number; its operation is similar to doing an (int)
type cast or by calling intval()
.
$x = '011';
echo +$x; // 11
The real question is why your code has an eval()
in the first place. Apart from modifying the string before it's evaluated, there's nothing much you could do about that.
If you're evaluating a mathematical expression, you could remove any leading zeroes before integer values like so:
$x = '5 * 011';
$x = preg_replace('/(?<=[^\d.]|^)0(?=\d+)/', '', $x); // "5 * 11"
echo eval("return (int)'011';");
Or what about changing it to:
echo eval("return 11;");
But if you want it to return 9 without changing the code itself, no this is not possible.