ID:               16227
 Updated by:       [EMAIL PROTECTED]
 Reported By:      [EMAIL PROTECTED]
 Status:           Open
 Bug Type:         Documentation problem
 Operating System: ANY
 PHP Version:      PHP4 only
 New Comment:

Oops,

$arr = array('a', 'b', 'c');

does reset position :)


Previous Comments:
------------------------------------------------------------------------

[2002-03-23 20:39:56] [EMAIL PROTECTED]

Both each() and foreach() uses internal hash position variable. 

Difference is foreach() reset internal hash position to the first
element while each() does not.

A problematic behavior is an assignment resets internal hash
position. There are other cases programmer has to be careful about
internal hash position and reference counting feature.

In addition to that, it is _not_ intuitive that assignment as follows

$arr = array('a', 'b', 'c');

does not reset hash posiiton, while following assignment does reset
hash position.

$arr2 = $arr1;

Anyway, some internal hash posision behaviors are inconsistent and
_not_ intuitive at all. This would be really hard to find if a user
experience the problem. Therefore, it should be documented :)



------------------------------------------------------------------------

[2002-03-23 07:44:26] [EMAIL PROTECTED]

OK, as a newbie PHP-er (experienced with Perl and Java) the
documentation I've been using suggested using that example before
foreach().  The foreach() behavior is free from the problem it seems,
even though documentation for foreach() on php.net claims that they
should be equivalent.  

Can anyone explain to this newbie why the (while list each) version was
the "intended behavior"?  Of course, having to use reset() seems kind
of odd to me anyway, but I wonder what was "wrong" with my first
code... PHP seems to look like it has Perl's "more than one way to do
it", but only in very limited areas. (I suppose if push came to shove I
could go back to using my own array walkers, but still...)

------------------------------------------------------------------------

[2002-03-23 07:10:07] [EMAIL PROTECTED]

Ok. Then this is documentation problem.
(I would like to hear from Andi or Zeev, though)

There should be note that internal hash position is reset if value is
assigned. (Both LVALUE and RVLAUE. They are the same value anyway.)

Also, reference counting side effect should be documented fully.

BTW, to fix this misbihavior, we need a additional variable to keep
track of hash position for each zval. (and change related codes) PHP3
does not have problem, since it does not have reference counting. 
 




------------------------------------------------------------------------

[2002-03-23 06:46:24] [EMAIL PROTECTED]

It's not a flaw, it's designed like this.

Derick

------------------------------------------------------------------------

[2002-03-23 06:39:14] [EMAIL PROTECTED]

Derick, 

foreach() does not solve this bug.
ZendEngine is resetting internal hash position by an
assignment. Therefore, foreach() does does not work.

i.e. The issue is _not_ "Not resetting hash posisiton", but
"Incorrectly resetting RVALUE hash position by an assignment".

This is serious flaw in language.


------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/16227

-- 
Edit this bug report at http://bugs.php.net/?id=16227&edit=1

Reply via email to