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 or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

PHP Arrays

php / introduction

Estimated reading time: 1 minute, 38 seconds

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:

<?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:

<?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:

<?php $beatles_instruments = array("John" => "guitar", "Paul" => "bass", "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:

<?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:


This could be shortened to:

<?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:

<form action="formhandler.php" method="post">
<legend>Please supply the following information</legend>
<label for="firstname" accesskey="f">First Name</label>
<input type="text" name="firstname" id="firstname">
<input type="submit" value="Submit">

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:

<?php echo $HTTP_POST_VARS['firstname']; ?>

This can also be shortened to:

<?php echo $_POST['firstname']; ?>
comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.