Posted by : senan Wednesday, February 12, 2014

Welcome! In programming, we often want to repeat a block of code to solve a problem. Instead of writting that block of code over and over again, we can use loops to iterate thorugh the code for however number of times we want.

Loops in PHP

Types of Loops in PHP
There are three types of loops often used while programming in PHP.
  • foreach loop
  • for loop
  • while loop
Let's go through each type with examples to a have better understanding on how to used them in PHP.

Foreach Loop

Foreach loop is most often used to print elements in a array.
Foreach Loop Syntax
foreach (array as $value)
{
 //code block to be run inside loop
}
On every iteration of a foreach loop, the value of the current array is assigned to $value.
Using Foreach Loop with Array
Let's imagine we want a drop down box list of our favorite browsers. We can use an array to store the names of the browsers and use PHP foreach loop in conjunction with HTML to make the drop down box. Let have a look at the code example to see how that is done.

Example 1 - Using foreach to print values in an array

<?php
 $browsers = array ("Firefox", "Internet Explorer", "Opera");
 echo "<select>";
 foreach($browsers as $browser) 
 {
  echo "<option name='$browser'>$browser</option>";
 }
 echo "</select>";
?>
The above code will output our drop down box.
Tip: If you're having difficulty understanding the code, i recommend you go through it slowly line by line to see what is happening.
Using Foreach Loop with Key - Value Pair Array
In last section, PHP Arrays, we learned multidimensional arrays. We can use foreach loop to print the keys and the values from a multidimensional array. What am i talking about?
Let say we have an multidimensional array of articles. In our array the article title is the key and the article body is the value. We will use foreach loop to print the articles onto our web page. Let see how that is done.

Example 1 - Using foreach to print key - values pairs in an array

<?php
$articles = array
(
 "PHP Variables" => "A variable is a mean to store values 
 such as strings or integers so we can easily 
 reuse those values in our code...",
 
 "PHP Strings" => "A string is a sequence of letters, symbols, 
 characters and arithmetic values...",
 
 "PHP Lopps" => "In programming, we often repeat an action or a 
 piece of code a number of times using loops 
 to solve a problem..."
);

echo "<table border='1'>";
foreach ($articles as $article_title => $article_body)
{
 echo "<tr>";
 echo "<td>";
 echo $article_title;
 echo "</td>";

 echo "<td>";
 echo $article_body;
 echo "</td>";
 echo "</tr>";
}
echo "</table>";

?>
So, in the above example we define an array of articles with key being the article title and value being the articly body. Next we use foreach loop to print the array keys and values in a table.
The operator "=>" represents the relationship between the key and value. So, in our example above, the $article_title is our key and $article_body is the value.
Output:
PHP VariablesA variable is a mean to store values such as strings or integers so we can easily reuse those values in our code...
PHP StringsA string is a sequence of letters, symbols, characters and arithmetic values...
PHP LoppsIn programming, we often repeat an action or a piece of code a number of times using loops to solve a problem...

The above example will print our articles in a table as below. Pretty neat eh? Loops can save a lot of time from doing repetitive things.
Now let's look at the next type of looping technique we use in php, the for loop and we'll see some examples of that.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Learning Programming Language - Skyblue - Powered by Blogger - Designed by Johanes Djogan -