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



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

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 '[]', 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.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: Linux X demo
    ... precedence to the order of evaluation. ... be related to anything such as parsing or precedence. ... THE ORDER OF EVALUATION BETWEEN SEQUENCE POINTS. ...
    (alt.lang.asm)
  • Re: Linux X demo
    ... precedence to the order of evaluation. ... evaluation of expressions is undefined. ... THE ORDER OF EVALUATION BETWEEN SEQUENCE POINTS. ...
    (alt.lang.asm)
  • Re: The annotated annotated annotated C standard
    ... Because precedence is not the same as order of evaluation. ... same atomic operation, increment it, ... in your frigging standard. ...
    (comp.programming)
  • Re: Function order of execution
    ... >> I know that Delphi is supposed to work from left to right. ... >Delphi evaluates operator precedence in a left to right order if they ... Any language with infixed operators (except for restricted cases such as ... A language *could* define the order of evaluation of the entities named ...
    (borland.public.delphi.language.objectpascal)
  • Re: Question on Operator Precedence
    ... Precedence has little or nothing ... to do with evaluation order. ... int x ... which suggests that the expression xis being evaluated first, ...
    (comp.lang.c)