PHP tutorial - How to loop in PHP


How To Loop In PHP

As you grow to a better coder, you will often met a situation where you have to execute a block of code several times in order to get a result. This is where looping is useful of!

The 'For' statement allows you to create loop that automatically execute desired blocks of code as many times as you like. To illustrate this, let say you want to create 100 lines of words. Instead of writting it one by one, you can ask PHP to do this for you. Fast and easy!

<?php

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

?>

The 'for' statement consist of three expressions.
  1. Initialization expression - This is used to set the initial value of the variable that is used to control looping process
  2. Conditional expression - Is used to determine how many times does this loop had to be done. In the example above, it is set to 100.
  3. Re-initialization ex皜ression - Notice at the double plus sign ( ++ ). This is an increment. It allows PHP to increase the value of 'Initialization expression' by one everytime the loop is executed.

There are several way to loop in PHP. The second method is to use while statement. It acts exactly like 'For' but only it allows you to create a condition. Thus, when condition returns to true, it will start the looping. Take the example above. Let say you are going to print out 100 words of text.

<?php

$x = 0;
while($x <= 100) {
echo "welcome<br>";
$x++;
}

?>

In this example, PHP will first assign 0 to variable $x. And then it will check whether the condition is true. What we want to do here is to ask PHP to print out the word 'welcome' as much as 100 times. Thus, when PHP read this line while($x <= 100) , it return to true because the number 0 is smaller than 100. So, the looping begin. This process continues until the condition return false.

When will it return false?
So, when do you think it will return false? As you use while loop, don't forget to add the increment sign at the bottom of the code. This double plus ( ++ ) sign allows PHP to increase the value of $x by 1 and will stop eventually when it reach 100. But if you did not add the increment sign, PHP will not know how many times that the process had been done and it will continue to loop endlessly as the value of $x remains the same.

The third looping method is 'foreach'. This is commonly used to print out the value of an array. I will talk about this when you have understand what is an array and how to loop it.

Try this Create me a looping that will print out 20 line of text alongside with the line number. It should looks like this

This is text 1
This is text 2
This is text 3
....... and so on!

  

Optimize Website - Webmasters optimize their site and gain higher search engine ranking

Webmaster tools - Free online webmaster toolkits, website reporting and free PHP opensource scripts.

Free Photo Hosting - Free File Hosting, Free ZIP file Hosting, Free Image Hosting, Free MP3 Hosting.

Funny Videos, Stupid Videos

Stupid Videos - Custom Pimped Motorcycle Pictures, Videos, and Articles

Planet Diaz - Programming Community - Online programming resource for programmers all around the world

Forectory - Webmaster Directory

St Louis Computer Networking

Website Templates - 1000s of Templates $5 - Many Templates to help you build your site!

 


Each and every tutorial published in PHPhelps.com is originally written by PHPHELPS OWNERS. None of the content is allowed to be replicated without prior notice to writter.
Copyright © 2006 PHPhelps.com