note 58582 added to control-structures.for
| From: | mbvlist at fastmail dot fm | Date: | Tue, 08 Nov 2005 20:37:52 +0000 |
| Subject: | note 58582 added to control-structures.for | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98273@lists.php.net to get a copy of this message | ||
While fuzzeling around with iterators (trying to find the next and previous item in a list),
discovered something odd:
<?php
for($i=0; $i < $max; $i++ && print 'increment,')
{
do_something();
echo " i = $i\n";
}
echo "finally, i = $i\n";
?>
If $max is set at 1 (only the first run will occur), it works as I expected:
i = 0
finally, i = 0
But with $max at a higher value (say 2) it works different:
i = 0
increment, i = 1
increment, finally, i = 2
After the loop was finished $i was incremented! As I read right now, this is what should happen. But
it doesn't occur in the first case. As I said, I was searching something using an iterator, and
after the loop I wanted to know the next value. Because of this 'feature' (a.k.a. bug)
that didn't work.
Be warned!
----
Manual Page -- http://www.php.net/manual/en/control-structures.for.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58582
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58582&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58582&report=yes
Search -- http://master.php.net/manage/user-notes.php