
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 while loop, do...while loop, for loop, and foreach loop. 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. <?php while (condition) { Code to be executed; SPONSORED BY: } ?> 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 <?php JavaScript $i = 7; SQL XML while ($i > 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 <?php Cool Tools $sql = "SELECT * FROM links"; Search Engine Marketing $query = mysql_query($sql); Ecommerce Domains & Hosting while($links = mysql_fetch_array($query)) { Web Publishing echo '<a href="'.$links['url'].'" target="_blank">'.$links['name'].'</a><br />'; 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 <?php Online Program. Learn More. do ipd.cdm.depaul.edu { Hearing Loop code to be executed; Experts } Systems from TV's to stadiums Equipment, while (condition); training and support ?> 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! <?php www.nitrosphere.net for (variable; condition; increment/decrement) Manage Sql Server { Monitor SQL Server code to be executed; Performance Memory Usage, Connection } statistics ?> www.manageengine.com We can take the while loop from above and make it one easy for loop: Code: PHP <?php for ($i = 7; ($i >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 <?php foreach (array as value) { code to be executed; } ?> 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 <?php $arr=array("one", "two", "three"); foreach ($arr as $value) http://www.lowter.com/article/php-loops[9/2/2010 2:01:50 AM] Lowter - PHP Loops { echo "Value: " . $value . "<br />"; } ?> 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].
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages3 Page
-
File Size-