PHP Statements and Beginning Loops - For Loop (Page 6 of 6 )
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...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |