HomePHP Page 6 - PHP Statements and Beginning Loops
For Loop - PHP
We discussed statements briefly in our last article and even got a sneak peek of an IF-statement. Sure I mean, the image was blurry, and the moment it saw us it ran off into the forest. But we saw it. Honest. So in this article, we're going to take a much closer look at PHP statements and even start learning about loops.
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.
$count_for_me = 0;
for ($i = 0; $i < 1,000,000; $i++)
$count_for_me += $count_for_me;
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...