Re: foreach vs. while(list() = each())
| From: | Plutarck | Date: | Thu, 12 Apr 2001 02:47:46 +0000 |
| Subject: | Re: foreach vs. while(list() = each()) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-48233@lists.php.net to get a copy of this message | ||
I believe foreach also works on a copy basis, not a pointer or reference. So
if you try and alter the array you are foreach'ing it won't work as
expected.
For instance this:
$array = array("var1", "var2", "var3");
foreach ($array as $val)
{
if ($val == "var2")
{
$val = "changed2";
}
}
print_r($array);
Will show you that no change to $array has occurred.
It's basically the same as list(), but it's worth noting.
Other than backwards compliance, I've never used the list/each way of doing
it. Never really had a reason too...I think it basically just comes down to
user preference.
--
Plutarck
Should be working on something...
...but forgot what it was.
"Joe Stump" <miester@php.net> wrote in message
news:20010411134931.C3680@zorka.com...
> What are the differences in these? I know with while() you have to reset()
the
> array afterwards, but foreach() you don't. Also foreach() appears to be
quite
> a bit faster.
>
> My main question is there ANY difference in how these two loop through the
> array.
>
> --Joe
>
>
>
>
/***************************************************************************
***\
> * Joe Stump - PHP/SQL/HTML Developer
*
> * http://www.care2.com -
> http://www.miester.org -
http://gtk.php-coder.net *
> * "Better to double your money on mediocrity than lose it all on a
dream." *
>
\***************************************************************************
***/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>