PHP Loops
![]() | Click on a Topic [ Hide ] |
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
| Learn PHP and MySQL fast! | ||
|
Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly... >> Order your copy now for just $39.95 << |
|
Types of Loops in PHP
There are three types of loops often used while programming in PHP.
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.
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.
| 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... |
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.
Are you still stuck using HTML?
"Learn how to build websites using PHP and MYSQL"
| << PHP Arrays | Top | PHP For Loop >> |






