demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

PHP Arrays

Arrays are special forms of variables. Whereas variables can only hold one value at a time – a string of text, a numeral, true or false – arrays can hold multiple values at the same time, each individually addressable. Variables are tiny delicate porcelain teacups; arrays are ice cube trays that can be expanded infinitely.

Arrays can be created for you – many system variables in PHP are actually arrays – or you can create them yourself:

  1. <?php $beatles = array(“John”,”Paul”,”Ringo”,”George”); ?>

In an ordinary array, each “slot” in the array is given a number, indexed from 0. To gain the name of the first member of the $beatles array, you would use:

  1. <?php echo $beatles[0]; ?>

It is also possible to create an associative array, in which words are used to label the slots, rather than numbers. Again, many of these are system variables that are created for you, but you can equally create your own keys for your own associative array:

  1. <?php $beatles_instruments = array(“John” => “guitar”, “Paul” => “bass”,
  2. “Ringo” => “drums”, “George” => “lead guitar”); ?>

To learn what instrument Paul played, you would echo $beatles_instruments[“Paul”]; where “Paul” is the key. To print out the entire contents of an array, use print_r:

  1. <?php print_r($beatles_instruments); ?>

As I mentioned, many server or system variables are arrays. There is an associative array for the browser used, as reported to the server when making a request of any document:

  1. <?php echo$HTTP_SERVER_VARS['HTTP_USER_AGENT']; ?>

This could be shortened to:

  1. <?php echo$_SERVER['HTTP_USER_AGENT']; ?>

Associative arrays are created from the name attribute value and entered data in forms when the user presses the submit button. So given this structure:

  1. <form action=”formhandler.php” method=”post”>
  2. <fieldset>
  3. <legend>Please supply the following information</legend>
  4. <label for=”firstname” accesskey=”f”>First Name</label>
  5. <input type=“text” name=“firstname” id=“firstname” size=“22” maxlength=“20” />
  6. <input type=”submit” value=”Submit” />
  7. </fieldset>
  8. </form>

When the submit button is pressed and formhandler.php is reached, the value of first name will be placed in an array. We can get this value on formhandler.php via:

  1. <?php echo $HTTP_POST_VARS[‘firstname’]; ?>

This can also be shortened to:

  1. <?php echo $_POST[‘firstname’]; ?>

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.