Foreach Statements
Written by admin on June 15th, 2007 in PHP Tutorials.
Although PHP can be good at outputting data you dont have to keep outputting the same data with a few different numbers or letters every time.
foreach is pretty logical to what it does. Basically its function is to keep going back to the start of a PHP code snippet until there is nothing else to execute.
It is similar to while but, foreach is just a couple of milliseconds faster.
The main use for foreach is for arrays. Now storing data in arrays, is simple but what about getting it out?
Well, with foreach this is how you would do it….
$MyArray = array(‘one’, ‘two’, ‘three’);
foreach($MyArray as $ArrayOutput) {
echo ArrayOutput;
}
Also foreach does not require you to specify the id of the information you want such as $MyArray[0].
Thats the basics of the foreach loop.