6
Contributors
13
Replies
74
Views
2 Years
Discussion Span
Last Post by JerrimePatient
Featured Replies
  • 1
    diafol 3,669   2 Years Ago

    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( … Read More

  • 1
    diafol 3,669   2 Years Ago

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

  • according to the manual: [operators precedence](http://php.net/manual/en/language.operators.precedence.php) it has an 'Undefined order of evaluation' and may 'change between versions of PHP or depending on the surrounding code' Read More

0

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.

Edited by Aeonix

0

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

Edited by diafol

0

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.

Edited by Aeonix

1

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!!

Edited by diafol

Votes + Comments
Neat tool.
0

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 :|

0

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.

3

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'

Edited by pzuurveen

Votes + Comments
Good link. Thought I was going insane. Well maybe I am, but not 'coz of this!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.