note 68189 deleted from control-structures.for by danbrown
| From: | danbrown@php.net | Date: | Mon, 07 Dec 2009 16:04:12 +0000 |
| Subject: | note 68189 deleted from control-structures.for by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163648@lists.php.net to get a copy of this message | ||
Note Submitter: thomas et luegger _dot_ de
----
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