PHP loops


Home - Tutorials - Basics

In this tutorial I will show you all loops in PHP and their usage with code examples.

Step 1 - PHP loops


PHP loops


PHP loops are control structures and you can use them the execute a code block more times. It means you don't have to copy and paste your code many times in the file just use a right loop statement.

A basic example:

Code:
  1. echo " 1 ";
  2. echo " 2 ";
  3. echo " 3 ";
  4. echo " 4 ";
  5. echo " 5 ";
  6. echo " 6 ";
  7. echo " 7 ";
  8. echo " 8 ";
  9. echo " 9 ";
And with a for loop it looks like this:
Code:
  1. for ($i=1;$i<=9;$i++){
  2. echo " $i ";
  3. }
And the output of both code is similar:
Output:
1 2 3 4 5 6 7 8 9
As you can see the sollution with loop is much better. It is shorter, more easy to understand. Besides this in most cases you don't know during the coding how many times the code block needs to be executed.

After this short example let's see what kind of loop statements are in PHP.
Besides this you can control your loops with:
In the next steps I will show you all of them.




Next Step of PHP loops


Tags: php loops, for loop, while loop, freach loop, loop

PHP loops - Table of contents
Step 1 - PHP loops
Step 2 - The PHP while loop
Step 3 - The do while loop
Step 4 - The for loop
Step 5 - The foreach loop

F1 Site Family
AJAX F1
CSS F1
Database F1
Forex F1
Flash F1
HTML F1
JavaScript F1
PhotoShop F1
PHP F1
 

 
MaxTutorial
Monthly mortgage payment calculator
WebFormGenerator
Forex mini accounts

Total time: 0.0143