Say you want to count to one million, but since you're too lazy, you decided to watch your computer do it instead. The For loop was designed specifically for slackers like you. <html> <body> <?php $count_for_me = 0; for ($i = 0; $i < 1,000,000; $i++) { $count_for_me += $count_for_me; } ?> </body> </html> I recommend changing the $count_for_me part to like 100 before running this program, otherwise you will be sitting there for a while. Breaking down the Code The For loop has three parts. The first part is the initializer, which initializes a counter variable. We set it to zero to execute the loop 1,000,000 times. If we set it to 1, it would only go through 999,000 times. The second part is the conditional part. This is the part where you give the program the criteria of how many times to run. It will keep running until this criteria is met. In this instance while $i is less than a million. The third and final part of the loop is the loop expression, which tells the program what action to take on the $i counter variable. In this case, we are incrementing it by one. Well folks, that's it for this tutorial. We will cover the rest of the loops in the next edition. Till then...
blog comments powered by Disqus |
|
|
|
|
|
|
|