PHP – For loops

Basic Layout

The for loop is a powerful thing when learning PHP it is one of the fundamental prerequisites for creating conditions and accessing data from a database such as SQL. The basic ‘for loop’ looks as follows.

1
2
for () {
}

Demo Loop

When the loop is loaded with content and a condition it will look something more like this:

1
2
3
for ($i = 0; $i <= 9; $i++) {
echo '<p>'.$i.'</p>';
}

Code Breakdown

After beginning to break the code apart we will find that the first line works as follows:

1
2
3
4
for ( //sets the beginning of the loop.
$i = 0; //tells us that the variable is set from 0 even though this is the default.
$i <= 9; //limits the loop so we don't have a string of numbers that never ends.
$i++) //tells the variable i to increment by 1.

When we run the code we will receive an output in our web browser that will look something like so:

1
2
3
4
5
6
7
8
9

This shows that the loop has worked through the conditions of ‘i.’ It simply sets $i to 0, adds 1, remembers the set variable, adds 1 again, remembers the new set variable and does so until the loops has reached its final condition which ends the loop. If you make a loops that does not end or you have a series of loops inside of each other that are very long, then you are going to have problems loading the web page. The most likely result will be a loading fail or a crash executing the script on the server.

Revise the Loop Structure

for (beginning variable; ending condition;increment)
{
Statements that will be executed for each time the loops runs.
}

Nesting the For Loop

It common for people to nest loops inside of loops to allow basic calculations, processes, or time. Say we wanted to create a multiplication table for 1′s that will only multiply up to 3 before changing to the next multiplication series.

1
2
3
4
5
6
for ($firstnumber = 0; $firstnumber <= 3; $firstnumber++) {
for ($secondnumber = 0; $secondnumber <= 3; $secondnumber++) {
$calc = $firstnumber * $secondnumber;
echo "$firstnumber x $secondnumber = $calc <br>";
}
}

Stepping Through the Code

Stepping through the code we recognize that the for loop begins and before closing the second for loop begins. Both of these loops will execute to give you a multiplication table; however, after the first loop runs through once the second loop will continue until done. When the second loop finishes it the loop reverts back to the parent and executes for the second round and thus the nested loop insides goes through 3 times again.

Results

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9

Related posts:

  1. Getting Secured Using Mysqli
  2. Redundant Data Class in PHP {Theory}
  3. Java: basic for and while loops
  4. PHP: Increasing a scripts runtime
  5. Random Images Using A PHP Function: cj_random_image
Tags: , , , ,
Caleb Jonasson

About: Caleb Jonasson

I am a web application developer currently spending my days coding at work, completing contracts and running around with my Nikon. This is my primary place for updates and everything code, technology and database related.