Home arrow PHP arrow Page 7 - PHP 101 (part 3) - Chocolate Fudge And Time Machines

What's That Noise? - PHP

After teaching you the fundamentals of form processing, PHP 101 returns with an explanation of WHILE, FOR and FOREACH loops, those PHP constructs that can save you hours of unnecessary HTML coding. Also included: array variables, the auto-increment operator, and some yummy desserts.

TABLE OF CONTENTS:
  1. PHP 101 (part 3) - Chocolate Fudge And Time Machines
  2. Back To The Future
  3. Revisiting The Past
  4. Doing It By The Numbers
  5. Anyone For Apple Pie?
  6. The Generation Gap
  7. What's That Noise?
  8. Checking The Boxes
  9. Miscellaneous Notes
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 15, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Of course, there is a simpler way of extracting all the elements of an array - PHP4 has a "foreach" loop designed specifically for this purpose and similar in syntax to the Perl construct of the same name. Here's what it looks like:


foreach ($array as $temp) { do this! }
or, in English, "take each element of the array variable $array, place it in the variable $temp, and execute the set of statements within the curly braces on $temp"

Let's demonstrate this by re-writing the example above in terms of the "foreach" loop:

<html> <head> <basefont face="Arial"> </head> <body> <? // define some array variables $then = array("The Beatles", "Roy Orbison", "Frank Sinatra"); $now = array("Britney Spears", "N-Sync", "Jennifer Lopez", "Blink 182"); ?> While Mom and Dad listen to <? // use loop to extract and display array elements foreach ($then as $artist) { echo $artist . ", "; } ?> in the living room, I'm grooving to <? foreach ($now as $artist) { echo $artist . ", "; } ?> in the basement! </body> </html>


 
 
>>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: