1. Home
  2. Computing & Technology
  3. PHP / MySQL

Learn PHP - A Beginner's Guide to PHP Programing

By Angela Bradley, About.com

5 of 9

Arrays

While a variable can hold a single piece of data, an array can hold a string of related data. Its use may not be apparent right away, but will become clearer as we start using loops and MySQL. Below is an example:

<?php
$friend[0] = "Justin";
$friend[1] = "Lloyd";
$friend[2] = "Alexa";
$friend[3] = "Devron";

$age["Justin"] = 45;
$age["Lloyd"] = 32;
$age["Alexa"] = 26;
$age["Devron"] = 15;

print "My friends names are " . $friend[0] . ", " . $friend[1] . ", " . $friend[2] . ", and " . $friend[3];

print "<p>";

print "Alexa is " . $age["Alexa"] . " years old";
?>

The first array ($friend) is arranged using integers as the key (the key is the information between the [brackets]) which is handy when using loops. The second array ($age) shows that you can also use a string (text) as the key. As demonstrated the values are called by print in the same way a regular variable would be.

The same principals apply to arrays as variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.)

5 of 9

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn PHP
  5. Learn PHP - PHP Tutorial - Arrays

©2008 About.com, a part of The New York Times Company.

All rights reserved.