note 92125 deleted from control-structures.for by danbrown
| From: | danbrown@php.net | Date: | Fri, 10 Jul 2009 12:10:55 +0000 |
| Subject: | note 92125 deleted from control-structures.for by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158145@lists.php.net to get a copy of this message | ||
Note Submitter: timmie dot loots at gmail dot com
----
@dkimbel13 at gmail dot com
First of all, I'm assuming the comma's you use in the example are supposed to be
semi-colons.
Secondly, I advise against using the example you posted (the for-loop using count() and next(). This
is due to the fact that there is no guarantee the entire array will be iterated. This is because the
result of current() can be false, even if you are not at the end of the array. This occurs if the
array contains a value that evaluates to false.
For example:
<?php
$array = array("value1", false, "value2", "value3");
for (reset($array); current($array); next($array)) {
echo "Element ".key($array)." contains
".current($array)."<br/>";
}
for ($i = 0, $size = count($array); $i < $size; $i++) {
echo "Element {$i} contains {$array[$i]}<br>";
}
?>