This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please upgrade to a browser that supports web standard_personals. It's free and painless.

Build A Website Blog

PHP Loops You Should Know

Skip | 22 November, 2005 17:34

The PHP programming language primarily has 4 loops:

* while()
* do{…}while()
* for()
* foreach()

An example of while loop is:

== Code begins ==

<?php

$keeplooping=0;
while($keeplooping<10)
{
    print "This is loop number " . ($keeplooping + 1) . ".<br>";
    $keeplooping++;
}

?>

== Code ends ==

The result of this code is:

This is loop number 1.
This is loop number 2.
This is loop number 3.
This is loop number 4.
This is loop number 5.
This is loop number 6.
This is loop number 7.
This is loop number 8.
This is loop number 9.
This is loop number 10.

As it is understood by the language, the while() loop first checks the condition, and only executes the commands within its periphery if the condition for the execution of the loop is satisfied. In the above case, the value of $keeplooping has to be less than 10 if you want the loop to happen. So if you had initialized $keeplooping = 10, the loop wouldn’t have executed. Now we apply the same code to the do{…}while() loop and see what happens.

== Code begins ==

<?php

$keeplooping=10;
do{
    print "This is loop number " . ($keeplooping + 1) . ".<br>";
    $keeplooping++;
}while($keeplooping<10)

?>

== Code ends ==

The result of this code is:

This is loop number 11.

The do{…}while() loop executes the lines at least once before checking for the condition. This loop first executes, and then checks, and the previous loop first checks and then executes.

Now we come to the for() loop. This loop is like the while(). The difference is, it generally initializes the loop controlling condition within its bracket. The following code explains this loop.

== Code begins ==

<?php

for($i=0; $i<10; $i++)
{
    print "This is loop number " . ($i+1) . ".<br>";
}

?>

== Code ends ==

The result of this code is:

This is loop number 1.
This is loop number 2.
This is loop number 3.
This is loop number 4.
This is loop number 5.
This is loop number 6.
This is loop number 7.
This is loop number 8.
This is loop number 9.
This is loop number 10.

The three parameters of this loop are self-explanatory. The first portion initializes the conditioning variable ($i=0), the second portion checks whether the condition is still conducive for the execution of the loop ($i<10) and the third portion keeps incrementing the variable until it exceeds the allowed limit ($i++). You can also leave one or more parameters blank such as:

== Code begins ==

<?php

for($i=0; $i<10;)
{
    print "This is loop number " . ($i+1) . ".<br>";
    $i++;
}

?>

== Code ends ==

gives you the same result as the previous example.

Lastly, we come to the foreach() loop. You can see an application of this loop at  http://www.aboutwebdesigning.com/2005/11/07/associative-arrays-in-php.
This loop is used while going through an associative array. Its syntax is like this:

== Code begins ==

<?php

foreach($ass_array as $key => $value)
{
    print "The index position right now is " . $key . " and its value is "
. $value . ".<br>";
}

?>

== Code ends ==

$ass_array is the associative array here. As you already know (explained in the above mentioned link) that associative arrays have string names as reference to their index positions, $key holds the string value of the index position. $value holds the actual value that the array holds at the $key index position.

About the author: Amrit Hallan is a freelance copywriter, and a website content writer. He also dabbles with PHP and HTML. For more tips and tricks in PHP, JavaScripting, XML, CSS designing and HTML, visit his blog at  http://www.aboutwebdesigning.com

Comments for post

 
Build A Website | Javascript | HTML Help | Persuasive Copywriting | HTML Form | Simple eMail Form | Build A Website Map