www.pickatutorial.com Computer Tutorials

Working with Arrays in PHP. Free online PHP and MySQL Web Database Programming Tutorial...

Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script
Lessons Working with Arrays in PHP Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Variables are very useful and we use them in almost every program. One inherent problem with variables is that they can store only a single value at a time. Oftentimes we need to store a list like data and if we use variables to store them, we need a large number of variables. This adds to the complexity of any program.

What is the solution?
We can use arrays to store multiple values in a single variable. Instead of having many similar variables, we can store the data as elements in an array. Each element in the array has its own ID so that it can be easily accessed.

An array in PHP is an ordered map. An array can be created by the array() construct. It takes a certain number of comma-separated values. An example is as under

$days = array("Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday");
echo $days[0]; // outputs Friday

The above code declares an array named $days, with seven elements. We can access the array elements using the [index] notation. Note that PHP arrays have 0 based indices i.e. the first element of an array is at index 0, second at 1 and son on.

We can also define the above array as under:

$days[0] = "Friday";
$days[1] = "Saturday";
$days[2] = "Sunday";
$days[3] = "Monday";
$days[4] = "Tuesday";
$days[5] = "Wednesday";
$days[6] = "Thursday";
echo $days[6]; // outputs Thursday




Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.