Re: Does passing an uninitialized array variable initialize it? (PHP 5)



On Tue, 31 Jul 2007 03:20:15 +0200, Jerry Stuckle <jstucklex@xxxxxxxxxxxxx> wrote:

Rik wrote:
On Tue, 31 Jul 2007 02:38:04 +0200, Jerry Stuckle <jstucklex@xxxxxxxxxxxxx> wrote:
I take this back - the result of the operation is unpredictable. That's because:

$rank[$i] = trim($rank[$i++]);

$rank[$i++]

is evaluated as $rank[$i]. However, when

$rank[$i]

is evaluated, does it use the old or the new version of $i?
This is indeed dubious. Which of the '[' gets evaluated first? Allthough, left-associtivity might indicate the first one should be evaluated first (PHP4 behaviour).
However, for:
$array[$i] = $i++;
Precedence as defined should kick in clearly. The '[' isn't in the table just for show.

The bottom line: don't change a value and use it in the same statement. Results are unpredictable.
And makes for more readable code indeed. However, if I read the documentation I fully expect the PHP4 result from my example, not the PHP5 one...
--Rik Wasmus

He, still trouble with my sig-seperator? :P

Rik,

The key here is, precedence defines the order of OPERATOR evaluation - but not the order of OPERAND evaluation. A subtle, but important difference in cases like this.

For instance, it defines that '++' will be evaluated before '[]',

Well, the other way around.

it doesn't define whether [$i] will use the old or new value of $i++. So even though [$i] is evaluated before [$i++], $i++ is evaluated first and the value of $i is unpredictable.

Hmm, you've got a point there. However, I still wonder what warrented the change from PHP4 to PHP5. Maybe it's got something to do with the fact references are more integrated, but that's just a wild guess.

What IS clear however, is that this is a nice example why one-liners are overrated. Nothing wrong with a few lines if it just clarifies things more, especially if it takes close to no extra CPU time and/or memory.
--
Rik Wasmus
.



Relevant Pages

  • Re: Does passing an uninitialized array variable initialize it? (PHP 5)
    ... left-associtivity might indicate the first one should be evaluated first (PHP4 behaviour). ... precedence defines the order of OPERATOR evaluation - but not the order of OPERAND evaluation. ... And I agree - while some one-liners are good, a good practice is to not reuse a variable you're changing in the same statement. ...
    (comp.lang.php)
  • Re: C/C++ Ambiguity in Order of Evaluation
    ... "Precedence and Order of Evaluation ... Expressions with higher-precedence operators are evaluated ... "Expressions with higher-precedence operators are evaluated first ...
    (comp.lang.c)
  • Re: Is a[i] = i++ correct?
    ... because of different parsing during compilation.Please explain ... Good afternoon, Jeniffer. ... The answer is that C does not guarantee order of evaluation. ... Therefore, i++ might be evaluated first, before being applied as an ...
    (comp.lang.c)
  • Re: Order of procedure argument eval?
    ... he was being pedantic in his reading of the question, ... or bar3 was evaluated first. ... parallel, in which case, no, none of them are evaluated first. ... even possible that "lazy evaluation" would result in none of them ever ...
    (comp.lang.fortran)
  • Re: Order of procedure argument eval?
    ... he was being pedantic in his reading of the question, ... or bar3 was evaluated first. ... parallel, in which case, no, none of them are evaluated first. ... even possible that "lazy evaluation" would result in none of them ever ...
    (comp.lang.fortran)