note 84667 deleted from control-structures.for by danbrown
| From: | danbrown@php.net | Date: | Thu, 24 Jul 2008 13:35:17 +0000 |
| Subject: | note 84667 deleted from control-structures.for by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142662@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
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