HomePHP Page 2 - PHP: The Switch Statement and Arrays
The Variable's Big Brother - PHP
In our last exciting adventure (back in early November), we braved crocodiles, ravenous editors, most of the PHP statements, and beginning loops. In this edition we'll cover the final statement, the Switch, and discuss arrays. So sit back, order your R2D2 robot to bring you a cold, frosty Jolt Cola, and let's get cracking.
Yeah you heard me. You'd better not mess with the variable, or he'll get his big brother, the Array, to lay the goon hand down on you. And you don't want that, because he is bigger, badder, and holds a lot more data.
Arrays in PHP are similar to the arrays in most programming languages. Like a variable, they hold data. Unlike variables, they hold more than one piece of data. They can hold as many as you like, what with their giant, rippling muscles. There are three types of arrays, and we are going to discuss them all here. They are the Numeric Array, the Associative Array, and my favorite, the squeezably soft Multidimensional Array.
Numeric Arrays
The name of the Numeric Array is a little misleading. When I first read it I thought they only stored numbers, which of course is wrong. The Numeric part simply refers to how the values in an array are stored. Other languages were smart enough not to name their arrays Numeric; I don't know why PHP wasn't.
The easiest way to think of an array is like a drawer with many file folders in it. Each file within the drawer holds data. If that is confusing, then gaze at my marvelous code, which will enlighten your puny brain:
In the above example, PHP automatically assigns the numeric index to your array. The value in the first position (Your Mother) gets the index of 0, the second position (Angelina Jolie) gets 1, and the third position (Whitney White) gets the index 2.
If we wanted to assign the index number manually, we could also write it this way:
$mygirlfriends[0] = “Your Mother”;
$mygirlfriends[1] = “Angelina Jolie”;
$mygirlfriends[2] = “Whitney White”;
Both ways store the information the same way; which is the best manner to assign the array values is up to you.