Home > Articles > Programming > PHP

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Looping

One situation in which the use of variables is crucial is in looping (repetitive processing). For example, part of displaying the calendar will be creating a link for each day. Ultimately, these links will be part of a table, but for now we'll create a table that consists of just one row, as shown in Listing 5.

Listing 5—Using a for Loop

<html>
<head><title>Event Calendar</title></head>
<body>
<?php

  $numberofdays = 31;

  $daysoftheweek = array("SUNDAY", "MONDAY", "TUESDAY",
              "WEDNESDAY", "THURSDAY", "FRIDAY",
              "SATURDAY");
?>
<h1>This Month's Events</h1>
  <table border="1">
    <tr>
<?php
  for ($i = 1;
     $i <= $numberofdays;
     $i++)
  {
     echo("<td>$i</td>");
  }
?>
    </tr>
  </table>
</body>
</html>

Notice that we've interspersed HTML and PHP code. You can include as many PHP sections as you want on a page, but in practice it's a good idea to avoid switching back and forth a lot.

Now let's take a look at the for loop in Listing 5. Within the parentheses are three statements:

$i = 1;

Initializes a new variable, $i, with a value of 1.

$i <= $numberofdays;

Defines an end state for the loop; processing will continue as long as the value of $i is less than the value of $numberofdays. When that statement is no longer true, the loop exits.

$i++)

Each time the loop executes, it increments the value of $i by 1.


The actions performed within the loop itself—in this case, just an echo() statement that outputs the value of $i within a td tag—are contained within the braces ({}).

Putting it all together, the loop starts in a state in which $i equals 1, increments $i by 1 each time it executes, and keeps going as long as $i is less than $numberofdays, outputting the current value each time.

The end result looks like Figure 4.

Figure 4Figure 4 Using a for loop.

This kind of loop is handy when you know exactly how many times you want the loop to execute, and when the index, represented here by $i, is predictable. Sometimes, though, you need more flexibility. For example, you might have an array with non-consecutive index values. In these cases, it's better to use a foreach loop, as shown in Listing 6.

Listing 6—Using a foreach Loop

...
  $daysoftheweek = array("SUNDAY", "MONDAY", "TUESDAY",
              "WEDNESDAY", "THURSDAY", "FRIDAY",
              "SATURDAY");
?>
<h1>This Month's Events</h1>
  <table border="1">
    <tr>
<?php
  foreach ($daysoftheweek as $thisday) {
    echo("<th>$thisday</th>");
  }
?>
    </tr>
    <tr>
<?php
  for ($i = 1; $i <= $numberofdays; $i++) {
...

A foreach loop executes once for each value within a group. For example, Listing 6 shows a loop that runs through the $daysoftheweek array. Each time the loop is processed, the next value within the array is assigned to $thisday, and the statements are executed. When there are no more values, the loop terminates. This way, we output each of the values within the array, as shown in Figure 5.

Figure 5Figure 5 Using a foreach loop.

Of course, this is hardly the way we expect a calendar to look! To get the layout right, we need to make two adjustments: Show the first day of the month as the correct day of the week, and create a new row for each week. Both of these tasks require functions.

  • + Share This
  • 🔖 Save To Your Account