On Sun, Mar 24, 2002 at 01:39:57AM -0000, [EMAIL PROTECTED] wrote : 
>  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:
> 
> 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,

    What do you mean with 'does not' ? You're creating a new
    array/hash which has by default it's internal position to 0.
    I think you can't speak of 'reset' here because there's
    nothing 'to reset'; you just created a new array and it's
    initialized to its default values (which means internal
    position == 0).

    Or did I miss the obvious ?!

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

    After making up my mind, I think which should discuss things
    here first.

    'documenting' now would mean, to me, documenting something
    which has not very intuitive but odd behaviour and should not
    be done that way in php (reseting RVALUE).

    'fixing' it would to most people I think make sense (read:
    RVALUE does not change it's internal hash position), but I
    cannot foresee the BC we might break.


    Another thing is, assiging an array to a new variable means
    it gets copied. So, for new behaviour, this would mean:

        LVALUE = copy of array, internal position set to zero
        RVALUE = left untouched, internal position left wherever it was


    whereas the current behaviour is:

        LVALUE = copy of array, internal position set to zero
        RVALUE = internal position set to zero


    A short example which shows the not-so-very-cool-beahaviour:

<?php
    $array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);

    while (list($key, $value) = each ($array)) {
        echo "$key => $value\n";
        $dummy = $array;
    }
?>

    Because of the line '$dummy = $array' this whole while() loop
    will turn into and endless loop.

    Who might have expecting this from a look at the code (and
    do not lie ;-)

    I'ld like to see someone coming up were the proposed new
    behaviour would break BC things as I currently can't make of
    any examples.



    So, what do others thinkg about this? Is it really better to
    stay with the current behaviour and document it that way?

    Or are there any obections to make the RVALUE assignment not
    reset the internal pointer (if it's implementation-wise
    possible, honestly it's out of my scope to answer this).


    regards,

        - Markus

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to