Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Proof That God Exists GameXe Christian Cosmos T-Tutorials 

Change Language

Who's online

There are currently 0 users and 13 guests online.

Lesson 13 - Intro to Arrays

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
//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');
?>

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

<?php
$array 
= array(Welcome);
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:

<?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"
?>

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:

<?php
$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:

developer
PHP.net
Tizag
Codewalkers


Submitted by David on November 9, 2006 - 11:54pm.
printer friendly version

Sweet

Very nice tuts here easily digestable

I am very new to php im i got variables etc down just want to get to the point where i write my own code with ease lol

How long does it take hmmmm

Thanks much


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati