fattwam Posted March 28, 2008 Report Posted March 28, 2008 Now it's time to study one of the most important parts of PHP - arrays. Every programming language in the world has them. Without them you would have millions and millions of loose variables in large scripts. Think of arrays as boxes that hold a lot of different variables. For example, think of your cupboard as an array containing all kinds of different foods. Instead of just having them all over the house the cupboard provides a place to put all of these different food so you can easily find them. That is like an array - instead of making 20 variables for every user account on your web site you could just make an array that would hold all 20 variables for each member. For example: //storing member values in variables: $user_one_age = "20"; $user_one_name = "Bob"; $user_one_url = "learnphpfree.com"; $user_one_phone = "909-555-5555"; $user_one_email = "junk@aol.com"; //storing member values in an Array: $member1 = array('20', 'Bob', 'learnphpfree.com', '909-555-5555', [email='junk@aol.com]'junk@aol.com[/email]'); ?> I'm sure you can see the time/space saving advantage right now . Then if you wanted any of these values you could print them out like this: echo $member1[0]; echo $member1[1]; echo $member1[2]; echo $member1[3]; echo $member1[4]; ?> which would print:20 Bob learnphpfree.com 909-555-5555 junk@aol.com Notice that arrays start with zero being the first value. However, arrays are much more powerful than this! There are MANY ways to make and use arrays in PHP. You will find as you start exploring other scripts that people usually don't use variables - as arrays are much more efficient. But by that same token, remember that arrays are merely groups of variables. One of the more popular forms of making arrays is to define the variable names in the array. By default, PHP gives each new item in the array a number starting at zero and going all the way to "infinity". For example, if you were to make an array of all of the letters in the word "Welcome" it would look like this: $array = array(W, e, l, c, o, m, e); echo $array[0]; //Prints "W" echo $array[4]; //Prints "o" echo $array[6]; //Prints "e" ?> (Notice that arrays start with zero being the first value.) However, rather than trying to remember the different variables numbers in an array you can give the variables names: $array = array("name" => 'Sam', "age" => '20', "url" => 'learnphpfree.com'); echo $array[name]; //Prints "Sam" echo $array[age]; //Prints "20" echo $array; //Prints "learnphpfree.com" ?> The array syntax is: array("the_name_of_the_var" => "the_value_you_want_it_set_to"); Giving each item a name helps people to remember how to call the values. An example might be an array containing all 150 products of an online store. Rather than trying to remember which product was number "97" in the array, you could name the product "black_sunglasses" instead: $products = array("black_sunglasses" => "Nice pair of Black Sunglasses"; echo $products['black_sunglasses']; //Prints: "Nice pair of Black Sunglasses" ?> In the next lesson I will show you several ways to make arrays. In the mean time you can read these articles to help you better understand arrays: developerPHP.netTizagCodewalkers
Recommended Posts
Archived
This topic is now archived and is closed to further replies.