PHP Coding Made Easier – Using for loops

21 Oct, 2011  |  Written by  |  under PHP, Tips and Tricks

php_loopsLoops, what are they you ask? Well, if you do not get to grips with PHP Loops you will find youself putting your head in one and jumping. :) Loops are frequently used in program, and they set up a block of statements that repeat. Sometimes, the loop repeats a specified number of times.

For instance, a loop to echo all the letters in my first name would need to repeat 5 times. Or how about echo the state capitals, that would require a repeat of 50 times. Sometimes, the loop repeats until a certain condition exists.

For instance, a loop that displays product information for all your products needs to repeat until it has displayed all the products, regardless of how many products you actually have.

Here are three types of loops to browse over:

  • Basic for loop: Sets up a counter; repeats a block of statements until the counter reaches a specified number.
  • while loop: Sets up a condition; checks the condition; and if it is true, repeats a block of statements.
  • do..while loop: Sets up a condition; executes a block of statements; checks the condition; if the condition is true, repeats the block of statements.

So, lets get started and take a look at Using for loops. The most basic for loops are based on a counter. You set the beginning value for the counter, set the ending value, and set how the counter is incremented.

The general format is as follows:

for(startingvalue;endingcondition;increment)
{
   block of statements;
}

So, now you might say, ok that looks simple enough but what do thos values mean? Lets take a look at those values now and see how we would fill those in:

  • startingvalue: A statement that sets up a variable to be your counter and sets it to your starting value. For instance, the statement $i=1; sets $i as the counter variable and sets it equal to 1. Frequently, the counter variable is started at 0 or 1. The starting value can be a combination of numbers (2 + 2) or a variable.
  • endingcondition: A statement that sets your ending value. As long as this statement is true, the block of statements keeps repeating. When this statement is not true, the loop ends. For instance, the statement $i<10; sets the ending value for the loop to 10. When $i is equal to 10, the statement is not longer true (because $i is not longer less than 10), and the loop stops repeating. The statement can include variables, such as $i<$size;.
  • increment: A statement that increments your counter. For instance, the statement $i++; adds 1 to your counter at the end of each block of statements. You can use other increment statements, such as $i+=1; or $i–;.

The basic for loop sets up a variable – for example, a variable called $i, – that is a counter. This variable has a value during each loop. The variable $i can be used in the block of statements that is repeating. For instance, the following simple loop displays Voice of Shaun Ellerton! three times:

for($i=1;$i<3;$i++)
{
   echo "$i. Voice of Shaun Ellerton!<br>";
}

PHP does not care whether the statements in the block are indented. However, indenting the blocks makes it much easier for you to understand the program.

The output from these statements is:

1. Voice of Shaun Ellerton!
2. Voice of Shaun Ellerton!
3. Voice of Shaun Ellerton!

for loops are particularly useful for looping through an array. Suppose that you have an array of customer names and want to display them all. You can do this easily with a loop as follows:

for($i=0;$i<100;$i++)
{
   echo "$CustomerNames[$i]<br>";
}

The output displays a Web page with a list of all customer names, one on each line. In this simple case, I have assumed that you have 100 customer names. But lets suppose you are sat there now saying:

Hey Shaun, I do not know how many customers are in my list!

Why not ask PHP how many values are in the array and use that value in your for loop. For example, you can use the following statements:

for($i=0;$i<sizeof($CustomerNames);$i++)
{
   echo "$CustomerNames[$i]<br>";
}

Notice that the ending is sizeof($CustomerNames). This statement finds out the number of values in the array and uses that number. That way, your loop repeats exactly the number of times that there are values in the array.

Remember!
The first value in an array with a numbered index is 0 unless you deliberately set it to a different number. One common mistake when working with arrays is to think of the first number as 1 rather than 0.

Do not forget my other blog posts regards using loops in PHP:

One Response so far | Have Your Say!

  1. john  |  December 22nd, 2011 at 9:32 am #

    thanks shaun, just starting to learn php and this has really helped cheers

    john - Gravatar

Leave a Feedback

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*