Neticulis.com > Programming > Learning PHP
>Tutorial Lesson 3:
Using Arrays

Introduction:

An array is a special type of variable that contains multiple pieces of data, these are very useful in programming and pretty easy to use too! You can make an array the same way you make any other variable, using the dollar sign ( $ ) to delcare its name, followed by an equals sign, then the array itself.

Step 1: Create a simple array.
To create an array, we will simply need to decide what we want to call it, and place some data into it. Lets make an array that contains the name of some search engines. So lets call this new array variable "searchEngines", check out line #4 in the code below:
1
2
3
4
5
6
7

<html>
<body>
<?php
$searchEngines = ["Google" , "Yahoo" , "Altavista"];
?>
</body>
</html>


Step 2: Print the data inside the array.
There are many ways to get at the data inside of an array, but for now I will just show you the most basic. Arrays use a number index, starting at 0, so if we wanted to get the first element of an array, we simple type $arrayname[0].
1
2
3
4
5
6
7
8
9
10

<html>
<body>
<?php
$searchEngines = ["Google" , "Yahoo" , "Altavista"];
print $searchEngines[0]."<br>";
print $searchEngines[1]."<br>";
print $searchEngines[2]."<br>";
?>
</body>
</html>


That is all for now, more coming soon!

Copyright 2005-2008 Neticulis.com