note 24231 added to control-structures.for
| From: | ninepro at cox dot net | Date: | Sat, 10 Aug 2002 04:26:42 +0000 |
| Subject: | note 24231 added to control-structures.for | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-34650@lists.php.net to get a copy of this message | ||
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:
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:
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
--
http://www.php.net/manual/en/control-structures.for.php
http://master.php.net/manage/user-notes.php?action=edit+24231
http://master.php.net/manage/user-notes.php?action=delete+24231
http://master.php.net/manage/user-notes.php?action=reject+24231