note 68189 added to control-structures.for
| From: | thomasetluegger_dot_de at osu1 dot php dot net | Date: | Mon, 17 Jul 2006 19:44:01 +0000 |
| Subject: | note 68189 added to control-structures.for | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114854@lists.php.net to get a copy of this message | ||
The previous post from Fatalerror is not correct. Variables enjoy no special treatment inside
'for' statements. At least I was not able to reproduce such a behavior. The code below
shows, that the value of $a from loop 1 is still valid outside the scope of it's loop. It can
be used without further initialisation for loop 2. So 'while' and 'for' loops
are just a matter of taste.
<?php
echo ("In loop 1: ");
for ($a = 0; $a < 3; $a++) echo("$a,");
echo ("\r\nAfter loop 1: a = $a");
echo ("\r\n\r\nIn loop 2: ");
for ( ; $a++ < 20; )
{
echo ("$a,");
if ($a==10) break;
}
echo ("\r\nAfter Loop 2: a = $a");
?>
output:
In loop 1: 0,1,2,
After loop 1: a = 3
In loop 2: 4,5,6,7,8,9,10,
After Loop 2: a = 10
Greetings, Tom
----
Server IP: 66.163.161.117
Probable Submitter: 82.83.69.134
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/control-structures.for.php
Edit -- http://master.php.net/note/edit/68189
Del: integrated -- http://master.php.net/note/delete/68189/integrated
Del: useless -- http://master.php.net/note/delete/68189/useless
Del: bad code -- http://master.php.net/note/delete/68189/bad+code
Del: spam -- http://master.php.net/note/delete/68189/spam
Del: non-english -- http://master.php.net/note/delete/68189/non-english
Del: in docs -- http://master.php.net/note/delete/68189/in+docs
Del: other reasons-- http://master.php.net/note/delete/68189
Reject -- http://master.php.net/note/reject/68189
Search -- http://master.php.net/manage/user-notes.php