October 20th, 2020

PHP Loops

What is a Loop?

Looping is simply a way for you to reduce the amount of code and/or content you have to write out. The idea is that you have something you have to do, or some content you have to show, and instead of just “writing it all out,” you find a pattern to it – a common denominator – and let PHP execute the code or generate the content piece by piece using that pattern, based on a condition.

A condition is a way of letting PHP know how many times to run your loop, or when to stop running your loop. The ability to take a condition and execute a set of instructions over and over as long as that condition is true is one of the basic cornerstones of programming.

To demonstrate how loops work, we will use a “for” loop to solve the following example problem:

“Joe wants to be able to take a range of numbers and find out how many numbers are divisible by a certain number, within that range of numbers. “

The Code

Okay the first thing I am going to do is give you the complete script for the solution to this problem. That way, you have the goods right up front and can refer to it as a whole.

  1. // variable to hold form
  2. $form = <<
  3. ‘{$_SERVER['PHP_SELF']}’ method = ‘post’>
  4. Range Start: ‘text’ name = ’start’ size = ‘3′>
  5. Range Finish: ‘text’ name = ‘finish’ size = ‘3′>
  6. Divided By: ‘text’ name = ‘num’ size = ‘3′>
  7. ’submit’ value = ‘Find Numbers’>
  9. // if we have posted info…
  10. if ($_POST) {
  11. // assign post vars to reg vars for easier handling
  12. $start = (int) $_POST['start'];
  13. $finish = (int) $_POST['finish'];
  14. $num = (int) $_POST['num'];
  15. echo “The following numbers from $start to $finish are divisible by $num : ;
  16. // loop from $start to $finish, 1 number at a time
  17. for ($x = $start; $x <= $finish; $x++) {
  18. // if the modulus of the current number and the divided by number
  19. // is zero, then it is evenly divisible by that number
  20. if (($x % $num) == 0) {
  21. // echo the number
  22. echo “$x ”;
  23. // end if
  24. // end for
  25. // end if
  26. // echo out the form
  27. echo $form;
  28. ?>

    What We Care About

    There may be a lot of things in that script you may be unfamiliar with. Basically there’s a form so we can get input from the user, and that’s about it. The only part of the script we are really going to focus on is this part:

    1. // loop from $start to $finish, 1 number at a time
    2. for ($x = $start; $x <= $finish; $x++) {
    3. // if the modulus of the current number and the divided by number
    4. // is zero, then it is evenly divisible by that number
    5. if (($x % $num) == 0) {
    6. // echo the number
    7. echo “$x ”;
    8. // end if
    9. // end for

      Let’s Get Loopy

      All loops are broken down into four basic pieces:

      - The initialization
      - The condition
      - The instruction
      - The iteration

      A basic “for” loop looks like this:

      for (initialization; condition; iteration) {

      We start out with telling PHP what kind of loop it is. Since it is a “for” loop, we start out by simply saying “for …” The initialization, condition, and iteration are put inside the ( )’s, separated by a semicolon. We use { } brackets to show where the start and end of our instruction is. Some people like to put the { on its own line like this:

      for (initialization; condition; iteration)

      It’s really just a matter of style; PHP doesn’t care either way.

    10. Going Around In Circles

      The Initialization
      This is where the loop starts. We tell the “for” loop where to start by assigning the starting number to a variable $x. In this example, our starting number is represented by $start – what the user puts in from the form.

      In a “for” loop, the initialization must always be a number. It doesn’t have to be a whole number, or even positive, but it has to be a number, and it has to be initialized by assignment to a variable, so that PHP can keep track of counting each iteration of the loop. In other words, it has to have this basic format:

      $x = $start;

      The Condition
      This is where we control how many times we want the loop to repeat, or iterate. A “for” loop counts from one number to some other number. This is where we tell PHP what that other number is. Without the condition, the loop will never stop repeating, and we would have what’s called an infinite loop.

      The condition must always check the initializing variable against a number value. It doesn’t have to be a whole number, it can be positive or negative, but it has to be a number value. You can use any of the normal condition operators for the condition. Some basic formats (that are relevant to our example) for the condition are:
      $x < $finish;
      $x <= $finish;
      $x != $finish;

      …And Again And Again

      The Iteration
      This is where PHP increments the initialization variable. In our example, we do $x++. This is just a shorthand way of doing $x = $x + 1, which is equally valid. it is also equally valid to increment it by something other than 1. It doesn’t even have to be a whole number. It can even subtract a number so your loop can “count down.”

      How you want your loop to count (the increments per loop, counting up or down, etc..) is entirely up to you, and you will base that on what your goal for the instruction is. In this example, we could just as easily start with the ending number and count down to the starting number, subtracting 1 from $x during each iteration of the loop, like so:

      1. for ($x = $finish; $x >= $start; $x–) {
      2. if (($x % $num) == 0) {
      3. echo “$x ”;
      4. // end if
      5. // end for

      On that note, be careful to “phrase” your loop correctly, or you will not get your desired results, or worse, end up with an infinite loop. Consider this loop:

      1. $start = 100;
      2. $finish = 200;
      3. for ($x = $start; $x >= $finish; $x++) {
      4. echo “$x ”;
      5. // end for

      We are telling the loop to start at 100, and while $x is greater than 200, run the instruction. Well the problem is, right out the gate, the condition is false, because 100 is not greater than 200. Our loop will never execute. Or how about this example:

      1. $start = 100;
      2. $finish = 200;
      3. for ($x = $finish; $x >= $start; $x++) {
      4. echo “$x ”;
      5. // end for

      Whoops, we accidentally put the start and finish variables backward. Since we start at 200, our loop will keep iterating as long as it is greater than 100. Each iteration, we add 1 to 200, so the condition will always evaluate as true. The loop will never stop, thus creating an infinite loop.

      Show Me The Money!

      The Instruction
      The Instruction is what you want PHP to do each iteration of your loop. It can be something as simple as echoing out the current iteration, doing some math based on each iteration, or pulling out a new row of information from a database. This is the “pattern,” or “common denominator” to your large chunk of code/content. It’s the individual brick that your loop stacks over and over to make your wall.

      The most challenging part of making a loop is the Instruction part. Finding the pattern isn’t always so easy as our simple condition to test if a number divides evenly by another number. You can use virtually anything inside your loop to build your pattern, from regular assignments/outputs to conditions; you can even put another loop inside your loop! And there’s really no limit to how far you can “nest” things inside one another, except for how far you can wrap your head around the logic. Finding a pattern to make a loop out of deserves it’s own tutorial altogether. It is what makes looping an artform.

      Raspberry Red, Lemony Lemon, Orangey Orange!

      Tucan Sam isn’t the only dude offering different kinds of loops. PHP offers four different ways to reiterate pieces of code:

      - The while loop
      - The do…while loop
      - The for loop
      - The foreach loop

      In principle, each kind of loop has the same basic components and does the same basic thing, but their nuances make it possible to write your code more efficiently, depending on your circumstance. The nuances in the loops are being able to put the loop’s components in different orders, or even combining components of the loop.

      Changing the order of the components is important to know, because this can and will change the outcome of your loop. For example, some loops or orders will execute the instruction first, then check the condition, so you will always get at least one iteration of the loop.

      The While Loop

      The basic While loop syntax looks like this:

      while (condition) {

      A working example might look like this:

      1. $x = 0;
      2. while ($x < 10) {
      3. echo “$x ”;
      4. $x++;
      5. }

      In essence, this while loop is exactly like a for loop. It has a set initializer, a set condition, and a set iteration. You are, of course, free to switch the order of the instruction and the iteration. In this example, it would actually be better to write this as a for loop, because that’s what the for loop was made for.

      The power of a while loop is being able to combine the initializer and even sometimes the iteration with the condition. One scenario in which this comes in handy is if you have a list of information in a database and you want to grab that information and make a loop to display that data, but you aren’t sure how many iterations there will be, because you aren’t sure how many rows will be returned from the database.


      1. $sql = “select name from table”;
      2. $result = mysql_query($sql);
      3. while ($name = mysql_fetch_assoc($result)) {
      4. echo “{$name['name']} ;
      5. }

      In this example, we run a query to select all the names in a table in your database. Inside the ( …), all 3 components (initialization, condition, iteration) are combined into a single expression.

      The initialization is easy enough to spot: $name = … is the initialization.

      mysql_fetch_assoc is a special function that grabs a name from the result source of the query and then internally increments the pointer of that result source to the next name in the results. This is the iteration, because when the loop finishes and goes back to the beginning, it will have a new name to fetch.

      The condition comes into play by the act of being able to initialize $name to the next name in the result source. If mysql_fetch_assoc gets to the end of the list of names, it will return false, so the next time the loop tries to assign a name to $name, it will fail, thus ending the loop.

      The Do…While Loop

      The do..while loop works pretty much exactly the same as the while loop, except that the instruction will always be executed first. The basic syntax looks like this:

      do {
      } while (condition);

      As with the while loop, putting the components in those positions makes it a glorified for loop, and you should use a for loop. Just like the while loop, the power is being able to combine the components and/or make a loop with an undetermined amount of iterations.

      If we were to use a do…while loop with the previous database example :

      1. $sql = “select name from table”;
      2. $result = mysql_query($sql);
      3. do {
      4. echo “{$name['name']} ;
      5. while ($name = mysql_fetch_assoc($result));

      In this example, we’re going to run into some problems. Because the instruction comes first, $name['name'] does not exist until after the first iteration of the loop. In my experience, there aren’t very many circumstances where a do…while loop is preferred over a while loop. It’s really only good for wanting to make a loop where you want the instruction to be executed at least one time, no matter what.

      The For Loop

      We used the for loop in our example on how loops work, so you should already have a decent understanding about this loop. The only thing to really understand about the for loop, is that it is the “strictest” loop out of the bunch. It needs a set starting point, and a set ending point. It’s useful if you know exactly how many iterations you are dealing with, simple as counting from 1 to 10.

      As shown previously:

      for (initialization; condition; iteration) {

      The condition will always be evaluated before the instruction is complete, and the instruction will be executed before the value is iterated.

      initialization -> condition -> instruction -> iteration -> condition -> instruction -> iteration …

      The Foreach Loop

      A foreach loop is specially designed for working with array elements. A basic structure of a foreach loop looks like this:

      foreach ($array as $value) {

      In this loop, the $value is the initialization component. The condition and iteration comes from the $array. The $array represents an array. The loop starts an internal pointer at the beginning of the array, and assigns the value of that element to $value, and then executes the instructions. When the instructions are done, the pointer is moved to the next element of $array (the iteration). If there is no element (the end of the array), the condition evaluates false, and the loop ends.

      Consider this example:

      1. $letters = array(‘a’,‘b’,‘c’);
      2. foreach($letters as $letter) {
      3. echo “$letter ;
      4. }

      The foreach loop points to the first element of the $letters array, executes the instructions, moves to the next element, etc… and when it runs out of elements, the loop is over. The output would be:


      The foreach loop accommodates associative arrays, as well. An associative array is an array that assigns a “name” to the element, or key, of the array. Example:

      1. $account = array(‘name’ => ‘John’, ‘number’ => 123, ‘age’ =>30);
      2. foreach($account as $key => $val) {
      3. echo “$key : $val ;
      4. }

      The output of this would be:

      name : John
      number : 123
      age : 30

      Continue and Break

      PHP offers two language constructs to break out of your loop: continue and break. “Continue” is used to skip the rest of the instructions in the current iteration of the loop. Let’s say you want to count from 1 to 100 but skip printing all the even numbers:

      1. for ($x = 0; $x <= 100; $x++) {
      2. if (($x % 2) == 0) {
      3. continue;
      4. }
      5. echo “$x ;
      6. }

      Each iteration of the loop we have a condition to check if $x is evenly divisible by 2. If it is, we skip echoing out $x and move on to the next iteration. Now we could have just as easily done this:

      1. for ($x = 1; $x <= 100; $x = $x + 2) {
      2. echo “$x ;
      3. }

      But that’s not the point! Besides, continue and break are useful for things much more complex than this. Break works the same way as continue, except that it breaks out of the whole loop, instead of the current iteration.

      Both break and continue can take an optional argument for you to specify what level loop to break or continue. This is useful for nested loops, where if you for example have a loop running inside another loop and if some condition is met, you want to exit out of the loop, even the main loop, you can do this:

      1. for ($x = 0; $x <= 10; $x++) {
      2. for ($y = $x; $y <= 10; $y++) {
      3. $z = $x * $y;
      4. echo “$x * $y = $z ;
      5. if ($z == 50) {
      6. break(2);
      7. }
      8. }
      9. }

      In this nested loop, we are printing out everything from 1 * 1 to 10 * 10 but we have a condition inside the nested loop to break out of all of the loops if we find an answer that equals 50.

      That’s All Folks!

      Well, that’s about all there is to know about loops. The basic syntax of loops is pretty easy to figure out; it’s finding the patterns to loop that’s the tough part. I hope that this has helped some of you out there who are just getting started and want to know what’s the point of loops and why are there different kinds. Perhaps in a future tutorial I will attempt to tackle tips and tricks to finding patterns for loop instructions.

      Until then, happy coding!

There are no comments yet, add one below.

Leave a Comment