Re: foreach problem part two [message #184281 is a reply to message #184279] |
Fri, 20 December 2013 01:09 |
Christoph Michael Bec
Messages: 207 Registered: June 2013
Karma:
|
Senior Member |
|
|
richard wrote:
> There is a flaw in the works that is not discussed.
> That being, it won't work with brackted arrays.
> Works fine with standard arrays.
Nonsense. PHP has only one array type.[1]
> While (){}. however works and could care less which is used.
Assume, you have an array:
$array = array('one', 'two', 'three');
Now you want to echo its elements in order. What is simpler and more
readable?
$i = 0;
while ($i < count($array)) {
echo $array[$i];
}
or
foreach ($array as $element) {
echo $element;
}
Additionally, the foreach loop is most likely faster.
[1] <http://www.php.net/manual/en/language.types.array.php>
--
Christoph M. Becker
|
|
|