Learning Loops in Php: FOR

Learning Loops in Php: FOR
by Janeth Kent Date: 11-04-2013

Loops come in several different flavors in PHP: for, while, do-while, and foreach. We will introduce you to each of them in 3 lessons and we will show you how they can making repetitive tasks straightforward and easy to maintain.

Lesson #1: The for Loop

 The syntax of a for loop is:

for (expr1; expr2; expr3)


The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).  Let’s consider the example of generating a multiplication table for the number 6. You could calculate and display the values resulting from multiplying 12 by 1, 2, 3 etc. like so:

<?php  echo "1 × 12 = " . (1 * 6) . "<br/>";  
echo "2 × 12 = " . (2 * 6) . "<br/>";  echo "3 × 12 = " . (3 * 6) . "<br/>";  ...

 Maybe extending the code to 6×6 would be quick, a simple matter of copy, paste, and amend. But what if the calculation needed to go up to 30×6? The copy and paste approach is really an inefficient use of your time and can lead to unwieldy code down the road.
A solution is at hand – the for loop.

<?php  for ($i = 1; $i <= 12; $i++) {      
echo $i . " × 12 = " . ($i * 12) . "<br/>";  

This example shows how few lines you need to create the multiplication table for any value.
There are three expressions used with a for loop which are separated by semicolons and contained within the parentheses:

  • The first expression $i = 1 initializes the variable $i to be used as a counter ($i is a commonly used variable name for loop counters). The counter will be used to keep track of how many times the loop has executed.
  • The second expression $i <= 6 determines whether to stop the loop or proceed. In this case, the loop will execute 6 times. To be more precise the loop will continue while $i has a value less than or equal to 6. Once $i has a higher value, the loop will stop.
  • The third expression $i++ updates the counter. $i is incremented by 1 each time after the loop as completed a cycle. A cycle of a loop is also known as an iteration.

Now what if you want to process in reverse i.e. multiplying 6 by 6, then 5, then 4, and so on? It’s easy!

<?php  for ($i = 12; $i > 0; $i--) 
echo $i . " × 12 = " . ($i * 12) . "<br/>";  

Enjoy the for loop! 

by Janeth Kent Date: 11-04-2013 hits : 1705  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.


We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration