PHP loops


Home - Tutorials - Basics

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

Tutorial info:


Name:PHP loops
Total steps:5
Category:Basics
Level:Beginner

Bookmark PHP loops



Bookmark and Share

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



Follow phpf1 on Twitter




F1 Site Family
AJAX F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.0146