Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 69
Comments: 20 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Using Loops in PHP
Using loops in conjunction with arrays is an excellent way to improve your PHP scripts. This tutorial will get your started with the basics of loops in PHP.

Loops are useful for many things. For one, they allow you to produce detailed lists of data much faster and easier than you could by manually typing them. Loops are also extremely useful when used with arrays of data.

So what exactly does a loop do? A loop is used to perform the same task until a specific condition is reached. There are a few different kinds of loops in PHP, like in any other programming language. In this tutorial I will cover the while, foreach, and for loops.

while loops

I will start with while loops as these are the most basic kind of loop in PHP. Here is what a while loop looks like:

<?php 

while(this_condition_is_true) {
    
// perform this task
}

?>

The structure of a while loop looks very similar to an if statement. PHP checks to see if what is inside of the parenthesis evaluates to true. If so, everything that is nested inside of the curly braces is performed. PHP continues to perform those tasks until the condition in the parenthesis evaluates to false.

Here is a very basic example of how to use a while loop:

<?php

// Let's count to 10!

$i 1;
while(
$i <= 10) {
    echo 
"$i<br />\n";
    
$i++;
}

?>

Alright, what's happening here is pretty simple. First off, we declare a variable, $i, that holds the value of 1. This variable is what we'll use for the conditional of our while loop as well as to echo out our numbers from 1 to 10.

We then tell PHP that we want it to perform a loop. Our loop will continue so long as the value of $i is less than or equal to 10 ($i <= 10). Each time PHP iterates through the loop we tell it to echo out our variable and then increment it by one. That's it, try running this script yourself to see what happens.

Go on to the next page to read about foreach loops.

« Previous [ 1 2 3 ] Next »
Discuss Tutorial: Using Loops in PHP 4 Comments
Comment by Wock on Jun 7, 2006, 10:20 pm
Wonny doesn't know that, haha, wock used this tutorail today for wormy. Tongue I used it for my birthday drop-down boxes Smile

Yay for wonny, Smile!!
Comment by Ron on Jun 8, 2006, 6:22 am
Great, Glad it helped you. Smile
Comment by Ferimer5 on Jun 12, 2006, 11:32 am
Great turorial! Wink Smile Grin Cool Tongue
Comment by Zach Sinclair on Jun 18, 2006, 3:00 am
ShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShockedShocked

I'm confused Ron... I'm going to have to practice this part of PHP alot to get the hang of it! Thanks for the tutorial Ron! Smile

« Previous [ 1 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.