Re: For Loops !
| From: | Steve Edberg | Date: | Mon, 31 Jul 2000 17:48:48 +0000 |
| Subject: | Re: For Loops ! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9208@lists.php.net to get a copy of this message | ||
At 3:08 AM +1000 8/1/00, Matt wrote:
Hello mailing list ! :) Although I have been programming for quite some time now I never really understood for loops even though I use them in almost every script. It has a strange little pattern and I always thought it had another meaning, is this true or is that just how a for loop is structured ?TMIYF! (The Manual Is Your Friend :) - http://www.php.net/manual/control-structures.for.php
for ($var=0;$var<10;$var++) { } Why does it use semi colons rather than commas ?Why not?
Is for some kind of function with those three params ? Hmm ... it's probably just me at 3am :)Yes, 3 am can do strange things... Anyway, the three statements - separated by semicolons - are evaluated as follows: The first is always executed once, before the loop (everything in the {...} block) begins. The second is evaluated at the beginning of each loop iteration. If FALSE, execution continues at the statement after the loop. The third statement is executed at the end of each loop, after everything in the {...} block. Any or all of these can be null. While the most common use of for is to execute a block of code with an incrementing or decrementing counter, that's not all it can do. The loop control statements can be ANY valid PHP statement. For instance: A 'Do forever' loop could be done via for (;;) { ... } Computing a factorial (that is, n!) could be done (I'm pretty sure...) via for ($i=$n-1; $i>1; $n=$n*$i--) {} Or you could do something weirder like for ($Var=$AnotherVar; $row=mysql_fetch_array($LinkId); $row['name']='') { ... } Note the use in statement 2 of the assignment '=' instead of the comparison operator '=='. This does work here, since the value of a statement is the value of the left side of the statement, and a null value is evaluated as false. If there's nothing to fetch, mysql_fetch_array() returns null, which is assigned to $row, the statement's logical value is false, and the loop exits. This would be equivalent to the more common structure of: $Var=$AnotherVar; while ($row=mysql_fetch_array($LinkId)) { ... $row['name'] = ''; } Mind you, these approaches might not might a lot of sense. But they all should execute. Lastly, the break and continue statements can be used to modify control flow: http://www.php.net/manual/control-structures.break.php http://www.php.net/manual/control-structures.continue.php - steve edberg And, hey, did I coin a new acronym?? Instead of RTFM, we can say TMIYF! Everytime anyone says 'TMIYF', send me a quarter ;) +--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+