<<

Lowter - PHP Loops Lowter

Home Articles Blogs Forums

Home » Articles » PHP Loops AUTHOR PHP Loops Ethan Poole BY ETHAN POOLE ON SUN 30 JAN 2005 AT 6:39

In all coding there comes a time when you have to execute a certain block of code a certain amount of times. Using loops

you can do this and it is quite easy. PHP supports four loops, the , do...while loop, , and . All are slightly different, but generically provide loops for various situations.

WHILE LOOP Ethan is a Scandinavian Studies student at the

While loops are useful and easy to use. The correct format for a while loop is: University of Minnesota in the Twin Cities. He is a PHP developer and the Managing Director Code: PHP of Lowter. Ethan is a crazy fan of the Opera web browser and he enjoys foreign language.

While loops will execute the specified the code block as long as the condition remains true. Conditional statements use the CATEGORIES condition operators, to learn about operators and variables (both necessary for looping) use PHP Variables and Operators. Code Here is an example of a properly coded while loop. PHP (X)HTML Code: PHP CSS 1) { Design echo "I is greater than 1"; Graphics $i--; Usability } Accessibility ?> Design Practice

The code above declares a variable $i. Using a while loop it makes a conditional statement saying that as long as $i is Programming less than 1 to execute the code block. The code block printing out the string will go 6 times. Application Design Frameworks The while loop is most used with MySQL to read the information from a database. An example of this being done would Design Patterns be a loop such as the one below: Application Security Code Syntax Code: PHP Miscellaneous '.$links['name'].'
'; Commentary } Server Setup ?>

A loop such as the one I previously posted uses a MySQL function that makes all the information collected from the SQL OPERA WEB BROWSER statement into an array for use in the code to be executed for each row of MySQL data. INNOVATION DELIVERED Download today! DO...WHILE LOOP

The do...while loop is very similar to the while loop, but it always executes the code block at least once and further http://www.lowter.com/article/php-loops[9/2/2010 2:01:50 AM] Lowter - PHP Loops

more as long as the condition remains true. The format for a do...while loop is: DePaul University Chicago Code: PHP SQL Server R2 DBA Certificate 11 Week www.wirelesshs.com Optimize SQL The loop you most likely won't use often, but in some situations it can be handy to know. Server 2008 do...while Optimize SQL Server, Save on Costs, FOR LOOP Immediate ROI, Free Trial Download! The for loop is useful when you know the exact amount of times that you want to execute the loop. It combines the www.Confio.com/SQL-Server variable creation, conditional statement, and increment/decrement all into the declaration of the start of the for loop, Is Your SQL Server technically these are the parameters. Slow? Network or WAN Code: PHP bottleneck? SQLNitro can increase bandwith ten fold! www.manageengine.com

We can take the while loop from above and make it one easy for loop:

Code: PHP

1; $i--;) { echo "I is greater than 1"; } ?>

For loops are useful, but yet the traditional while loop is easy to comprehend and is great for MySQL.

FOREACH LOOP

The foreach loop is specifically for looping through the values of an array. It loops over the array and each value for the current element of the array is stored in the specified variable and the array pointer is advanced by one to go the next element in the array. This loops you through the entire array.

Code: PHP

The foreach loop can also be used for MySQL, but yet the while loop is much easier to use. An example of a foreach loop would be:

Code: PHP

foreach ($arr as $value) http://www.lowter.com/article/php-loops[9/2/2010 2:01:50 AM] Lowter - PHP Loops { echo "Value: " . $value . "
"; } ?>

Foreach loops are another useful loop that you will use when storing large amounts of data in arrays.

SUMMARY

Most of the loops above you'll use sometime in your coding, and the most unlikely one that you will use is do...while loops. The rest are used often and make coding much easier. Now you can do the most useful task with loops going through MySQL databases allowing you to create powerful PHP applications with database interaction.

Tags: php loops

About Lowter RSS Feeds Advertisements Advertisements About Us All Content Linux Reseller Hosting Credit Counseling Advertise Articles Web Design Company Arizona Landscaping Submit Content Blog Entries Phoenix Landscaping Syndication Comments Renegade Motorhomes Terms and Conditions Forums

Copyright © 2004-210 Lowter. All rights reserved.

http://www.lowter.com/article/php-loops[9/2/2010 2:01:50 AM]