PHP
  Home arrow PHP arrow Page 5 - PHP 101 (part 3) - Chocolate Fudge And Time Machines
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (part 3) - Chocolate Fudge And Time Machines
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2000-08-15


    Table of Contents:
  • PHP 101 (part 3) - Chocolate Fudge And Time Machines
  • Back To The Future
  • Revisiting The Past
  • Doing It By The Numbers
  • Anyone For Apple Pie?
  • The Generation Gap
  • What's That Noise?
  • Checking The Boxes
  • Miscellaneous Notes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP 101 (part 3) - Chocolate Fudge And Time Machines - Anyone For Apple Pie?
    ( Page 5 of 9 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT