Re: foreach vs. while(list() = each())

From: Date: Thu, 12 Apr 2001 01:36:57 +0000
Subject: Re: foreach vs. while(list() = each())
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-48225@lists.php.net to get a copy of this message
One apparent problem with foreach() is it can misbehave code as follows. (4.0.4pl1, 4.0.5RC6) function foo($a) { foreach ($a[0] as $k => $v) { echo $k.$v; } } $a = 'abc'; foo($a); You'll get 'server not found' or browser waiting forever with this code. (If you don't, please let me know) String can be accessed like array, but foreach does not handle invalid parameter well. While() does not misbehave with equivalent code. Regards, -- Yasuo Ohgaki "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 >

« previous php.general (#48225) next »