note 84667 added to control-structures.for
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 24 Jul 2008 13:26:50 +0000 |
| Subject: | note 84667 added to control-structures.for | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142647@lists.php.net to get a copy of this message | ||
I have noticed an oddity with the for construct. This code will *not* work as expected:
$minimum = 1;
$maximum = 5;
for ($i=$minimum; $i<$maximum; $i++) {
echo "This is iteration ".$i."\n";
}
This will output the following (in an infinite loop):
This is iteration 1
This is iteration 1
This is iteration 1
...
I assume this is because variables are not 'expressions', although I can't find any
documentation that would explain why this does not work. To fix this, you can put double-quotes
around the variables, like this:
$minimum = 1;
$maximum = 5;
for ($i="$minimum"; $i<"$maximum"; $i++) {
echo "This is iteration ".$i."\n";
}
This results in the expected output:
This is iteration 1
This is iteration 2
This is iteration 3
This is iteration 4
This is iteration 5
----
Server IP: 66.207.199.35
Probable Submitter: 209.82.43.120
----
Manual Page -- http://www.php.net/manual/en/control-structures.for.php
Edit -- https://master.php.net/note/edit/84667
Del: integrated -- https://master.php.net/note/delete/84667/integrated
Del: useless -- https://master.php.net/note/delete/84667/useless
Del: bad code -- https://master.php.net/note/delete/84667/bad+code
Del: spam -- https://master.php.net/note/delete/84667/spam
Del: non-english -- https://master.php.net/note/delete/84667/non-english
Del: in docs -- https://master.php.net/note/delete/84667/in+docs
Del: other reasons-- https://master.php.net/note/delete/84667
Reject -- https://master.php.net/note/reject/84667
Search -- https://master.php.net/manage/user-notes.php