PHP: Loops

Sometimes when we're coding, we want to do something that would be very tedious. This is where loops come in. Loops repeat a series of instructions for you for a specified number of times.

For Loops

For example, let's say we wanted to see list out the presidential elections for this century. Sure, we could use echo statements for 2000, 2004, 2008, etc. Or, we could do a simple For Loop as shown below.

<?php
      for ($Prez = 2000; $leap < 2100; $Prez = $Prez + 4) {
        echo "<p>$Prez</p>";
      }

After the "for" command, inside the parentheses, we tell PHP three things separated by selicolons. 1) Where to start the loop, 2) where to end the loop, and 3) what to do to the next instance of the loop. After that, between the curly braces, we tell PHP what command to execute (which in our case is echoing the variable.

ForEach Loops

ForEach loops are similar to the above but work for each element of an object, such as an array. For example, let's create an array with the names of the main characters of The Simpsons and use ForEach to echo their names out.

<?php
          $Simpsons = array("Homer",
          "Marge", "Bart",
          "Lisa", "Maggie");
        
          foreach ($Simpsons as $Family) {
              echo "<li>$Family</li>";
          }
        
          unset($Family);
        ?>

Just like we've seen before, we begin with an variable representing an array. Then we use a foreach loop and set the old variable Simpsons temporarily to a new variable called Family. Then we echo each object in the Family array (which is actually the Simpsons array) as a line item. Finally, we unset the Family variable with the unset command. If this were part of a larger script, if we wanted to call the array again, we could just call $Simpsons.

While Loops

You may have already noticed that For and ForEach Loops work well when you know how many times you need to repeat a command (such as echo, like we've been doing. But what if you don't know how many times you need to repeat loop? This is where While Loops come in. A While Loop will execute a given command for a loop as long as a specified condition is met. For example, the While Loop will run as long as a condition is false or a condition is true, etc. Let's look at a simple example.

<?php 
$x = 30; 

while($x <= 37) {
    echo "The number is: $x <br>";
    $x++;

?>

We start by saying X = 30. Then we echo X as long as X is less than or equal to 37. After our echo statement, we add 1 to the variable X, as shown in the $x++ statement.

Do/While Loops

A Do/While Loop is will usually work just like a normal While Loop. The difference is that a Do/While Loop checks the condition AFTER it runs through the loop and will repeat if the condition allows it to, rather running through the loop only if a condition is met. For example, we could use a Do/While Loop to accomplish the same result as the While Loop above.

<?php 
$x = 30; 

do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 37);
?>

The results from those two loops would be the same. However, if we set X to be higher than the condition, it would only run as a Do/While Loop.

<?php 
$x = 38;

do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 37);
?>

If we tried doing this using a While Loop, then the loop would not run because the condition is not met.

Comments

Popular posts from this blog

Installing CentOS 7 on a Raspberry Pi 3

Modifying the Zebra F-701 & F-402 pens

How to fix DPM Auto-Protection failures of SQL servers