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

Anyone For Apple Pie? - 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
PHP offers one more type of loop, the "foreach" loop, which is designed specifically for use with array variables. Logic would suggest that we explain array variables before we attempt to teach you the "foreach" loop...and you know what slaves we are to logic!

Thus far, the variables you've used contain only a single value - for example,


$i = 0
However, array variables are a different kettle of fish altogether. An array variable can best be thought of as a "container" variable, which can contain one or more values. For example,


$desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake");
Here, $desserts is an array variable, which contains the values "chocolate mousse", "tiramisu", "apple pie", and "chocolate fudge cake".

Array variables are particularly useful for grouping related values together - names, dates, phone numbers of ex-girlfriends et al.

The various elements of the array are accessed via an index number, with the first element starting at zero. So, to access the element


"chocolate mousse"
you would use the notation

$desserts[0]
while

"chocolate fudge cake"
would be

$desserts[3]
- essentially, the array variable name followed by the index number enclosed within square braces. Geeks refer to this as "zero-based indexing".

Defining an array variable is simple via the array() function - here's how:


$past_flames = array("Jennifer", "Susan", "Tina", "Bozo The Clown");
The rules for choosing an array variable name are the same as those for any other PHP variable - it must begin with a letter, and can optionally be followed by more letters and numbers.

Alternatively, you can define an array by specifying values for each element in the index notation, like this:


$past_flames[0] = "Jennifer"; $past_flames[1] = "Susan"; $past_flames[2] = "Tina"; $past_flames[3] = "Jennifer";


 
 
>>> 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 5 - Follow our Sitemap

Dev Shed Tutorial Topics: