.
Tutorial
Avatar

PHP Basics - Loops

By Remco on 2009-04-28
Views: 595

Introduction

This tutorial is about loops, what are they, why use them, and how to use them.

What is a loop? and why use it?

A loop is a sequence of commands that is executed repeatedly.
But why would that be useful...

I will give you an example:

I want to make a select box with 20 numbers in it.
Ofcourse i can write the HTML code for it:

Code:


<select>
<option value='1'>1
<option value='2'>2
<option value='3'>3
<option value='4'>4
<option value='5'>5
...
</select>


But i could also use a loop to just count for me, and do it automaticly.
( examples of loops come later on )

You can use these loops for almost anything you can think of.
Display your pages dynamicly, read your database tables etc.

Different type of loops and how to use them

There are severeal types of loops you can use, and they are all different.
For example:
  • For loop
  • While loop
  • Foreach loop


For loop
The for loop is very useful when you are counting or working with numbers.
I will give an example, then explain what it is doing:

Code:


<?php

for($i=0;$i<10;$i++)
{
    echo 
$i."<br>";
}

?>



What the code above basicly means is:
As long as variable $i is smaller than 10, you execute the code and then count up $i with 1.

So you could do this to create 10 textfields and make them unique:

Code:


<?php

echo "
<form action='' method='post'>"
;
for(
$i=0;$i<=10;$i++)
{
    echo 
"<input type='text' name='".$i."'><br>;
}
echo "
</form>";

?>



This code will display 10 textfields.

While loop
The while loop sounds pretty obvious.
While "Condition"... do this code, I often use while loops to read my database tables. i will give you an example of it:

Code:


<?php

// in this case ofcourse connect to your database.

$get_users mysql_query("SELECT * FROM users");
while(
$row_users mysql_fetch_array($get_users))
{
    
// As long as the query contains another row of information...
    
echo $row_users['username']."<br>";
}

?>



This code will output all usernames of all your users, not just one.
The while loop just checks wether it's keeping itself to the condition you give to it yes or no, if it is it will keep running and if not it will just stop.
another example:

Code:


<?php

$total 
0;

while(
$total !== 10)
{
    echo 
"I don't equal 10 yet <br>";
    
$total ++;
}

?>



Foreach loop
The foreach loop is a little bit different than the others.
This loop is specially made for array's.
The loop will execute the code you fill in for every record in your array, we will give you an example:

Code:


<?php

$array 
= array('part1','part2','part3');
foreach(
$array as $part)// For each record in the database, give $part the value.
{
    echo 
$part."<br>";
}

?>




This code will output:
part1
part2
part3

Ofcourse you will have to use your own creativity to use these loops.
Anything is possible if you use them wisely.
If there are any questions, please contact me by mail or a comment.
« Previous Next »




Comments

You have to be logged in to write a comment.

.