1. Computing & Technology

Discuss in my forum

Loops in PHP

By , About.com Guide

What is a loop?:

You've heard the term loop before, but you may not really be sure what it is. A loop is a series of commands that will continue to repeat over and over again untill a condition is met. For example, let's say you have the names of people in an array, and you wanted print a list of all of the names. You could setup a loop that would print the first persons name, and then move on to the next and print her name, etc (the continuing to repeat itself part.) The condition would be that it stops once all of the names have been used.

FOREACH Loop:

FOREACH Loops are used to do something specific to each value in an array. So for example if an array contained 5 pieces of data, then the FOREACH loop would execute 5 times. Read about FOREACH Loops[/link">

FOR Loop:

WHILE Loops:

Basically what a WHILE loop does is evaluate a statement as true or false. If it is true it executes some code and then alters the original statement and starts all over again by re-evaluating it. It continues to loop through the code like this until the statement becomes false. Read about WHILE Loops

Loop Decisions:

While inside the loop, decisions about what to execute are sometimes made based upon the current loop data. The most common ways to do this are to use an IF/ELSE statement, or to use SWITCH to make a Switch Loop.
Related Searches term loop single line array

©2012 About.com. All rights reserved.

A part of The New York Times Company.