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


Total Votes: 62
Comments: 18 — 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.

for loops

For loops are the most complex loops in PHP. The syntax is a little different from a while loop. There are three different expressions contained within the parenthesis of a for loop, as shown below:

<?php

for(expression1expression2expression3) {
    
// tasks to perform
}

?>

The first expression is performed at the beginning of the loop, no matter what. This is similar to declaring the "$i" or similar variable with while loops.

The second expression is evaluated at the beginning of each iteration. If it evaluates to true, the loop continues and the actions between the curly braces are executed. This is similar to the conditional of while loops.

The third expression is performed at the end of each iteration. The most commonly used expression here is something similar to "$i++" Do you see how for and while loops are somewhat similar?

Let's try the same example we used for while loops:

<?php

// Let's count to 10!

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

?>

Do you see how for loops sort of compact everything inside of the parenthesis? The variable declartion, conditional statement and incrementing of the variable are all done inside of the parenthesis. This can make your code smaller and more efficient.

You may be wondering what loops are useful for. After all, any idiot can count to ten without PHP's help. Loops are most effective when used in combination with arrays. You can use loops to iterate over each item in your array giving you virtually limitless opportunities.

I'll show you an example. You could possibly even modify this code to make your own comment system.

<?php

// First we'll build an array of "comments"
// I just made these up, by the why. =P

$comments = array(
    array(
        
'name' => 'Ron',
        
'mesg' => 'Hello World!',
    ),
    
    array(
        
'name' => 'Visitor',
        
'mesg' => 'This is a great site!',
    ),
    
    array(
        
'name' => 'Passer-by',
        
'mesg' => 'I like hot pockets.',
    ),
);

$total_comments count($comments);

for(
$i=0$i<$total_comments$i++) {
    
$name $comments[$i]['name'];
    
$mesg $comments[$i]['mesg'];

    echo 
"<p>Comment by $name: $mesg</p>";
}

?>

This example uses a multidimensional array. Each item in the $comments array is an array itself, containing a name and a message item. We use a for loop to iterate through each item in the $comments array and echo the name and message of each comment. This is a really simple example, but it does demonstrate one good use of for loops.

That's all for this tutorial. In summary: We covered three types of loops in PHP; while, foreach and for. Experiment with each of them and if you have any questions or comments, feel free to post them below.

« 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.