The FOREACH Loop Love Affair with Mr. Array
Remember our good friend the array? Well one thing I forgot to mention about Mr. Array is that he is in an eternal love affair with Mrs. FOREACH Loop. I know this site is normally about web development, but some gossip about our function friends is always important. In fact, when beginning development on any new site, you should always create personnas to be representative of your users to make sure you are really addressing their needs! I’ll talk more about personas another day though, for now les get back to our lovers quarrel…!
Mrs. FOREACH Loop (yes, you need to yell FOREACH really loud, then say Loop quietly, that’s how it’s spelled and as developers we need to follow standards!) is not like our other loop friends , the incredible FOR loop and the magical WHILE loop. Mrs. FOREACH Loop does not have a statement that runs alongside a counter that continues until the statement is proven false. The FOREACH Loop continues until it has used all values contained within an array. Some might say that Mrs. FOREACH Loop is therefore a bit of a gold digger… !
It’s a pretty simple concept. If the array contains one million items, the FOREACH loop will execute one million times. Here is an example of what a FOREACH Loop looks like:
<?php
$MrArray = array(1, 2, 3, 5, 8);
foreach ($MrArray as $MyLove)
{
print $MyLove . "\n";
}
?>
In the above example, Mrs. FOREACH Loop prints all of Mr. Arrays numbers! Don’t they make a cute couple?
If you’ve got any dirt on any other PHP functions, please do not hesitate to report your finding immediately. I’d love to get some screenshots up of Mr. Array and Mrs. FOREACH at the bar or something…
May 29th, 2007 at 8:32 am
[...] FOREACH Loops [...]
May 31st, 2007 at 2:44 pm
[...] Persona’s During Development May 31st, 2007 by Deceth A few days ago I mentioned persona’s and promised to go into more details on a future date. Today, I am happy to announce that we have [...]
November 26th, 2007 at 1:04 pm
[...] The FOREACH Loop Love Affair with Mr.Array [...]