Sometimes you'll want to store more than one value within your variables. With an array, you can do just that! Arrays allow you to store multiple values within a variable.
For example, I think moustaches are neat. There are many kinds of moustaches! Using individual variables, I can store each moustache in their own, like so:
$moustache1 = "Handlebar"; $moustache2 = "Salvador Dali"; $moustache3 = "Fu Manchu";
But, I'd like to keep things neat n' tidy and have all my moustaches in a single variable. So I'll use an array, like so:
$moustaches = array("Handlebar", "Salvador Dali", "Fu Manchu");
Each value is automatically assigned a "key" in the array, so we can grab a specific value when we need it. We'll touch on keys later, but by default, each value has a numeric key assigned to it.
So for example, "Handlebar" is 1, "Salvador Dali" is 2, and "Fu Manchu" is 3. Here's the catch though — the numbers start at 0, not 1. So, "Handlebar" is actually 0, etc.
If we want to grab a value out of an array and display it on our web page, we just reference the array and the key associated with the value we want to display, like so:
<?php echo $moustaches; // this will display "Handlebar" echo $moustaches; // this will display "Salvador Dali" echo $moustaches; // this will display "Fu Manchu" ?>
Arrays are pretty powerful, and there's a lot more to them. So stay tuned!