Tabs left
Home
Scripts
Tutorials
Forums
Info Left
Info Right
Content Gradient Left
Top Scripts and Tutorials!
Scripts:
Simple JavaScript Clock
Rating: 3/5
A simple javascript clock!

FlatFile Hits Counter
Rating: 3/5
Use this script to see how many people have visite

Tutorials:
Using the date function in ActionScript 3.0
Rating: 3/5
How to use the date function in ActionScript 3

PHP loops
Rating: 3/5
What are PHP loops, and how to create them.

Get Firefox
Ubuntu - Snazzy Valid XHTML 1.0 Transitional
Valid CSS!
PHP loops

Files

None.

Instructions | Troubleshooting

Difficulty: Easy
Time it takes to complete: 10 minutes
Rating: 3/5 (232 total votes)            (Rate this tutorial!)

Instructions:

Today we will learn about loops and how to make them. Surely, you will often need to repeat a certain piece of code, so that what loops are for. Luckily, in PHP we have 3 simple statements to do it:

  • while: Loops through a block of code while a condition is true.
  • do while: Executes a bunch of code, and then repeats it while a condition is true.
  • for: Just loop a code x number of times.
  • foreach: This one loops a bunch code for each element in an array.

Ok, let's explain each of them:


The while statement

This statement will execute a block of code if and as long as a condition is true.

$i = 5;
while ($i<=15) {
	echo "We're currently executing code number".$i." of the loop.<br />";
    $i++;
}

The do while statement

This statement is very similar to while, except that it first executes the code no matter the condition, and THEN loops it while the condition is true.

$i = 0;

do {
    $i++;
	echo "We're currently executing code number".$i." of the loop.<br />";
}
while ($i<=10)

The for statement

The for statement is used when you know how many times you want to repeat a loop.

for ($i=0;$i<=10;$i++) {
	echo "I ate ".$i."chocolate bars!";
}

The foreach statement

At last but not least, comes the foreach statement. This famous code is used to loop through arrays. The number of loops is based on how many elements the array contains.

$array = array("blue","red","purple","yellow","green");

foreach ($array as $colors => $number) {
	echo "The ball has those colors: <br />"
    echo $number." - ".$colors;
}

Congratulations! Now you know how to repeat code efficiently! See you in the next lesson, here at WebDevHQ.

Troubleshooting:

Infinite loops

Please note, that it is possible to create loops that never end. Those are called infinite loops. They'll usually make your script crash. Just remember that you shouldn't be ashamed, since everyone that knows advanced PHP has made an infinite loop, even me. It's common, but with practice you'll make fewer of them, and spot them easily.


About the 'for' statement

Remember that this statement has 3 parameters: The first one defines the variables, the second holds the condition, and the third explains what to do after it's excecuted (usually used for incrementing a variable).

By: Danneh

^^ Back to top ^^

Custom Search
Content Gradient Right