Get support from Xavier Media
It is currently Sun Dec 08, 2013 3:44 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Lesson 13 - Intro to Arrays
PostPosted: Thu Jul 13, 2006 7:53 pm 

Points:
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:

[php] <?php

//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', 'junk@aol.com');

?>[/php]

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:
[php] <?php

echo $member1[0];
echo $member1[1];
echo $member1[2];
echo $member1[3];
echo $member1[4];

?>[/php]

which would print:
Code:
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:

[php] <?php
$array = array(W, e, l, c, o, m, e);
echo $array[0]; //Prints "W"
echo $array[4]; //Prints "o"
echo $array[6]; //Prints "e"
?>[/php]
(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:

[php] <?php
$array = array("name" => 'Sam', "age" => '20', "url" => 'learnphpfree.com');
echo $array[name]; //Prints "Sam"
echo $array[age]; //Prints "20"
echo $array[url]; //Prints "learnphpfree.com"
?>[/php]


The array syntax is:

Code:
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:

[php] <?php
$products = array("black_sunglasses" => "Nice pair of Black Sunglasses";
echo $products['black_sunglasses']; //Prints: "Nice pair of Black Sunglasses"
?>[/php]




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:

developer
PHP.net
Tizag
Codewalkers


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]