Home arrow PHP arrow 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.

  1. PHP Statements and Beginning Loops
  2. If
  3. Else
  4. Elseif
  5. Here We Go Loop de Loop
  6. For Loop
By: James Payne
Rating: starstarstarstarstar / 5
November 05, 2007

print this article



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...

>>> More PHP Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: