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

Recommended Answers

All 13 Replies

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 for diafol

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 for diafol

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 for diafol

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 for diafol

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.

No body can't solve my problem?

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 for diafol

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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.