$x=1;
echo $x + $x++ + $x++ + $x;

Compiler Mode - Engaged

$x = 1;                         int x = 1;
echo $x + $x++ + $x++ + $x;     print x + x++ + x++ + x;
                                int temp[n][] = 1;
                                int temp[n][] = 1 + 1;
                                int temp[n][] = 1 + 1 + (temp[x]);
                                int temp[x][] += x;
                                int temp[x][] == 2;
                                int temp[n][] = 1 + 1 + 2;
                                int temp[n][] = 1 + 1 + 2 + (temp[x]);
                                int temp[x][] += x;
                                int temp[x][] == 3;
                                int temp[n][] = 1 + 1 + 2 + (temp[x]);
                                int temp[n][] = 1 + 1 + 2 + 3;
                                print temp[n];
                                print 1 + 1 + 2 + 3;
                                6
echo $x;                        print 3;
$x = 1;
                                <<EOF

Does this answer satisfy you?

If you try to put "functions" I wrote on the right side in any programming language, it will go bad. But everybody knows what print, int etc. mean.

Answer is 8 not 6

commented: Aha! +15
Member Avatar

I thought it was 9 :D but it is 8

Interesting, I assumed it would be 1 +2 + 3 + 3 however it seems to be 1+2+2+3

Answer is 8 not 6

Yea, I found this answer later (after I posted the post) and while I was editing post, DaniWeb stopped responding for like next 30 minutes, and, it refuses any edits later. So it remains with "kind of" wrong answer.

Member Avatar

This is the code syntax tree from http://phpfiddle.org/

array(
    0: Expr_Assign(
        var: Expr_Variable(
            name: x
        )
        expr: Scalar_LNumber(
            value: 1
        )
    )
    1: Stmt_Echo(
        exprs: array(
            0: Expr_Plus(
                left: Expr_Plus(
                    left: Expr_Plus(
                        left: Expr_Variable(
                            name: x
                        )
                        right: Expr_PostInc(
                            var: Expr_Variable(
                                name: x
                            )
                        )
                    )
                    right: Expr_PostInc(
                        var: Expr_Variable(
                            name: x
                        )
                    )
                )
                right: Expr_Variable(
                    name: x
                )
            )
        )
    )
)

I did this:

$x = 1;

$sum = $x;

echo "SUM = $sum,  x = $x" . '<br />';

$sum += $x++;

echo "SUM = $sum,  x = $x" . '<br />';

$sum += $x++;

echo "SUM = $sum,  x = $x" . '<br />';

$sum += $x;

echo "SUM = $sum,  x = $x" . '<br />';

And got:

SUM = 1, x = 1
SUM = 2, x = 2
SUM = 4, x = 3
SUM = 7, x = 3

So now I have 7, 8, 9 (1+2+3+3) as answers. Not being a CS guy, I have no idea about this, anybody help?

//EDIT

Forget it, just got it from the php manual:

$a++    Post-increment  Returns $a, then increments $a by one.

So if you wanted the pre-increment, you need to use ++$x.

I think that's a gotcha for me. Gonnna have to check some stuff!!

commented: Neat tool. +1

Still not getting how it is doing?

Member Avatar

I can get 4 different answers every time, heh heh!

There is no failure in computing, only in human thinking, if you run the PHP code you receive an 8.

That means every other way of thinking is wrong, except one that refers to 8.
Unfortunately for me, I'm wrong :|

Member Avatar

There is no failure in computing, only in human thinking, if you run the PHP code you receive an 8.
That means every other way of thinking is wrong, except one that refers to 8.

Obviously.

according to the manual: operators precedence

it has an 'Undefined order of evaluation'
and may 'change between versions of PHP or depending on the surrounding code'

commented: Good link. Thought I was going insane. Well maybe I am, but not 'coz of this! +15
Member Avatar

No body can't solve my problem?

This is abhi10kumar's problem. We're just along for the ride.

$x=1;
echo $x + $x++ + $x++ + $x;

x = 1 + 2 + 2 + 2;
x = 7;