PHP Arrays


You know what a variable is – just a storage area where you hold numbers and text. The problem is, a variable will hold only one value. You can store a single number in a variable, or a single string. An array is like a special variable, which can hold more than one number, or more than one string, at a time. If you have a list of items (like a list of customer orders, for example), and you need to do something with them, then it would be quite cumbersome to do this:

$Order_Number1 = "Black shoes";
$Order_Number2 = "Tan shoes";
$Order_Number3 = "Red shoes";
$Order_Number4 = "Blue shoes";

What if you want to loop through your orders and find a specific one? And what if you had not four orders but four hundred? A single variable is clearly not the best programming tool to use here. But an array is! An array can hold all your orders under a single name. And you can access the orders by just referring to the array name.

If that's a bit confusing right now, let’s make a start on explaining how arrays work.