note 24231 deleted from control-structures.for by tomsommer
| From: | tomsommer@php.net | Date: | Thu, 05 Aug 2004 18:28:37 +0000 |
| Subject: | note 24231 deleted from control-structures.for by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74070@lists.php.net to get a copy of this message | ||
Note Submitter: ninepro@cox.net
----
something i found out about FOR loops. it may be obvious to most, but im sure not obvious to all.
if you have nested FOR loops, you cannot use the same counting variable. example:
<?php
for ($i=0;$i<=12;$i++) {
for ($i=0;$i<=40;$i++) {
}
}
?>
that example is wrong because the second FOR loop keeps adding to the $i variable until it hits 41.
Once the second FOR loop is finished, the first FOR loop goes to evaluate $i and sees that $i is now
41 and does not continue.
This is my example of one way to do it correctly:
<?php
for ($i=0;$i<=12;$i++) {
for ($ii=0;$ii<=40;$ii++) {
}
}
unset($i, $ii);
?>
hope this saves someone a few hours of frustration :)
-rick