VisageHosting.com
 
Home
 
Resources
- PHP and MySQL (2)
- HTML and CSS (0)
- CGI and Perl (0)
- Java (0)
- JavaScript (1)
- Python (0)
- ASP (1)
- Web Hosting/Servers (14)
 
Articles
- PHP/MySQL (11)
- HTML/CSS (5)
- Other Languages (4)
- Web Hosting (2)
- Computer Tech (7)
 
Forum
 
 
Register
Login

Username:

Password:

 
 
Currently 0 registered users and 3 guests browsing.

29 articles available for viewing.

1313397 total page hits.

Welcome to our newest registered user, choonchan!


PHP/MySQL -> Tutorials



PHP Syntax (Control Structures): while, do-while and for Loops

- by surreal on 05/28/06, 4:42AM CEST (3348 Views)



This article is a runner up to the article PHP Syntax (Control Structures): if, else and elseif.

In this article I'm going to teach you how useful loop statements are in PHP.

The logic behind a loop is really quite simple. Just like in an if statement, the loop will only run if the conditions are true. The difference is, the loop will run a second time if the conditions are still true, and a third, and will continue to loop until the conditions are no longer true.

The while loop

The while loop is the simplest form of a loop in PHP. It follows the exact logic that I outlined above, so lets outline what's happening with a bit of code:


Code:
$counter = 0; // Here we define a numeric variable

while ($counter < 10) { // Here the loop will execute as long as $counter is less than 10 and will stop looping when $counter is equal to 10.

print"<br />{$counter}"; // This will print the value of $counter on each loop.

$counter++; // This tells PHP to add one to the value of $counter.

}


If we started $counter at a value greater than or equal to 10, the loop would not run at all and we'd have no output. If we had $counter <= 10 for the conditions, there would be one more loop than $counter < 10.

You must be very careful with what your conditions are in a while loop, as if you set conditions that will always be true, the loop will be infinite and will just keep looping into you go to stop the script from executing. This is not a good idea as it will consume a lot of bandwidth and will basically stop your script from functioning how it should.

The do-while loop

The do-while loop is very similar to the while loop, but with two key differences. The do-while's conditions are set at the end of the loop, which means it will always loop at least once no matter what the conditions are. If on the second loop the conditions are not true, the loop will stop.

Here's an example:


Code:
$counter = 0;

do {

print"<br />{$counter}";

$counter++;

} while ($counter < 10); // The conditions are set at the end of the loop.


As you can see the do-while loop will function very similarly to the while loop. The above will output identically to the while loop example above.

Using the do-while loop with MySQL

The do-while is very useful when dealing with the results of a MySQL query. We can check if any results have been returned in the query, and then use the do-while loop to output any results that have been found:


Code:
$result = mysql_query("select * from articles order by date_time desc");

if ($row = mysql_fetch_array($result)) {

do { // Results returned, so execute the loop.

print"<br />The author of this article is {$row["author"]}!";

} while ($row = mysql_fetch_array($result));

} else { // No results returned for the query.

print"<br />No articles to display!";

}


The for loop

The for loop is the most complicated form of loop in the whole of PHP. They behave identically to for loops in C programming (if you've ever experienced these, you will understand how to use them in PHP).

The for loop is basically made up of 3 expressions in the following syntax:

for (expression1; expression2; expression3)

This means that we can set 3 actions in the conditions without having to define them elsewhere. A good example of this is below:


Code:
for ($counter = 0; $counter < 10; $counter++) {

print"<br />{$counter}";

}


The above will output identically to the above while and do-while examples, but as you can see it is much more condensed because we've defined everything in the conditions of the loop. This only scratches the surface of how useful for loops are. In my experience, for loops are very useful for manipulating and outputting the content of arrays, but I will go into more detail on that in another article!

That's about it for loops (no pun intended). In my next article I will be teaching how to use require()s.

Feel free to comment if you have any questions!


Seen this article somewhere else? Report it for plagiarism!

Noticed an error in this article? Tell us what's wrong!

View Comments for this Article

Related Articles

- PHP Syntax (Control Structures): if, else and elseif

- Compiling and Executing Your Java Program

- Basic Syntax of PHP

- Basic Syntax of a Java Program

- Prerequisites for Java Programming
Rating

5.00/5

1 Votes

Want to rate this article? Register a free account or Login if you already have one.


Comments

Comment by choonchan on 08/11/10, 4:49PM CEST

TNfndG [a href="http://woynvplfzsnb.com/"]woynvplfzsnb[/a], [url=http://xblppbyxnrka.com/]xblppbyxnrka[/url], [link=http://qumheuuqtgie.com/]qumheuuqtgie[/link], http://cfoqgestgnwf.com/

Comment by lymanknap on 08/04/10, 9:46PM CEST

V6UdOD [a href="http://majqrqxxfbym.com/"]majqrqxxfbym[/a], [url=http://bruriqbthezk.com/]bruriqbthezk[/url], [link=http://pqurashpviic.com/]pqurashpviic[/link], http://nxqrlpnjvswe.com/



Want to add a comment? Register a free account or Login if you already have one.



All content on this site is property of www.juxtaposing.com. Do not copy anything on this site without prior written permission of the site owner.