PHP
  Home arrow PHP arrow Page 6 - 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 - The Generation Gap
    ( Page 6 of 9 )

    You can add elements to the array in a similar manner - for example, if you wanted to add the element "apricot fritters" to the $desserts array, you would use this:

    $desserts[4] = "apricot fritters";
    and the array would now look like this:


    $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters");
    In order to modify an element of an array, you would simply assign a new value to the corresponding scalar variable. If you wanted to replace "chocolate fudge cake" with "chocolate chip cookies", you'd simply use


    $desserts[3] = "chocolate chip cookies";
    and the array would now read

    $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate chip cookies", "apricot fritters");
    PHP arrays can store both string and numeric data - in fact, the same array can store both types of data, a luxury not available in many other programming languages.

    How about a quick example?


    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <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 for ($x=0; $x<sizeof($then); $x++) { echo $then[$x] . ", "; } ?> in the living room, I'm grooving to <? for ($y=0; $y<sizeof($now); $y++) { echo $now[$y] . ", "; } ?> in the basement! </body> </html>
    And here's what you'll see:


    While Mom and Dad listen to The Beatles, Roy Orbison, Frank Sinatra, in the living room, I'm grooving to Britney Spears, N-Sync, Jennifer Lopez, Blink 182, in the basement!
    In this case, we've first defined two arrays, and then used the "for" loop to run through each one, extract the elements using the index notation, and display them one after the other. Note our usage of the sizeof() function - this function is typically used to return the size or length of an array variable, and is used here to ensure that the loop iterates as many times as there are elements in each array.

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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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