1

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

Apolo
  • 3,844
  • 1
  • 21
  • 51
  • It's not octal it is decimal value. – Think Different Jul 07 '14 at 10:31
  • I can't think of a *sane* way… but I can't think of a reason why you would be evaluating PHP but not wanting to do so following the normal rules of PHP. This sounds horribly like an XY problem. – Quentin Jul 07 '14 at 10:31
  • 1
    There's no way to force it from such expression. You must change something within evaluated `return 011` line to achieve different results. – mip Jul 07 '14 at 10:33

4 Answers4

3

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.

Update

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"
Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • why eval : because `$x` can be an arithmetic expression. – Apolo Jul 07 '14 at 10:40
  • @Apolo I suppose that's fair; could you share a little more code about how that would come about? – Ja͢ck Jul 07 '14 at 10:41
  • For some reasons I can't share the code, but just for you to try it : imagine you have an array full of arithmetic expressions and numbers can have leading zeroes. You want the result of every expressions in a new array. – Apolo Jul 07 '14 at 10:51
  • @Apolo I've updated my answer to reflect something that might work for you. – Ja͢ck Jul 07 '14 at 10:51
  • @Apollo Who writes decimal `11` in such way? Shouldn't `011` be considered as octal? This could be actually desired functionality. – mip Jul 07 '14 at 10:54
  • @doc I must deal with it – Apolo Jul 07 '14 at 10:57
  • @apolo they're look around assertions, i.e. match 0 if not preceded by another digit or period and if followed by another digit ( to prevent removing a single 0 – Ja͢ck Jul 07 '14 at 11:00
  • Your regular expression performs weird if value is in brackets, i.e. `5 * (-001 + 2)` -> `5 * (-01 + 2)`. – mip Jul 07 '14 at 11:08
  • @doc that's not weird, it just doesn't deal with multiple consecutive zeroes; not sure whether that's a requirement though. – Ja͢ck Jul 07 '14 at 11:11
  • Ah true, dunno how I've seen it removing consecutive zeroes :o. Must be tired. – mip Jul 07 '14 at 11:13
  • I hope this time my eyes are not fooling me. `011 * 5` -> `011 * 5` – mip Jul 07 '14 at 11:28
  • BTW is unary `+` operator (`+$x`) documented somewhere? I can't see it in PHP manual. – mip Jul 07 '14 at 11:40
  • @doc Ugh yes, missing `^` in the look behind, will edit when I get back to my desk – Ja͢ck Jul 07 '14 at 11:42
  • @doc The unary use of `+` is like the `-` in the sense that you can think of it as an imaginary `0` on one side of the operator. – Ja͢ck Jul 07 '14 at 11:51
  • @Jack I modified your regex to `'/(?<!\d)0(?=\d+)/'` yours wasn't working for 0 at the start of the expression. eg. with '011' :) – Apolo Jul 07 '14 at 12:39
1

Did you mean intval() ?

<?php echo intval('011');// will return 11?>
Prabowo Murti
  • 1,216
  • 2
  • 18
  • 27
0
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.

Marek
  • 7,337
  • 1
  • 22
  • 33
0

Add the (int) to change the data type to integer.

echo (int)'011';
Allen Chak
  • 1,802
  • 1
  • 10
  • 21