PHP Variables

While a PHP scalar variable stores a single value, an array variable can be used to store a set or sequence of values. PHP supports numerically indexed arrays and associative arrays. An array in PHP is actually an ordered map. A map is a type that maps values to keys. Array variables consist of two parts - an index and an element. The array index, sometimes referred to as the array key, is a value used to identify or access array elements. The array index is placed in square brackets. Most arrays use numeric indices that typically start with 0 or 1. In PHP associative arrays can use string indices. Both types of arrays are created using the array() construct.

Numerically Indexed Arrays

$my_array = array('red', 'green', 'blue')

This code creates a numerically indexed array called $my_array. The array is assigned three elements - red, green, and blue. Each element is identified by a numeric index.

$my_array[0] = 'red' //index 0 corresponds to element red
$my_array[1] = 'green' // index 1 corresponds to element green
$my_array[2] = 'blue' // index 2 corresponds to element blue

To access the contents of an array, use the array name and index. The following code is used to display the values of the $my_array variable.

<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A Web Page</title>
</head>
<body>

<p>

<?php 

$my_array = array('red', 'green', 'blue');

echo "The first value of the array is " . $my_array[0];
echo "The second value of the array is " . $my_array[1];
echo "The third value of the array is " . $my_array[2];

?>

</p>
</body>
</html>

The first value of the array is red
The first value of the array is green
The first value of the array is blue

Associative Arrays

Associative arrays allow you to use more useful index values. With numerically indexed arrays, index values are created automatically beginning with 0. Associative arrays permit numeric and string index values. The symbol between the index and values (=>) is an equal sign immediately followed by a greater than symbol.

$members = array('FName' => John, 'LName' => Smith, 'Age' => 50)

In this example, the array members contain three elements, however the string indices - FName, LName, and Age are used.

$members['FName'] = 'John' //index FName corresponds to element John
$members['LName'] = 'Smith' // index LName corresponds to element Smith
$members['Age'] = '50' // index Age corresponds to element 50

To access the contents of an array, use the array name and index. The following code is used to display the values of the $members variable.

<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A Web Page</title>
</head>
<body>

<p>

<?php 

$members = array('FName' => John, 'LName' => Smith, 'Age' => 50);

echo "The user's first name is: " . $members['FName'];
echo "The user's last name is " . $members['LName'];
echo "The user's age is " . $members['Age'];

?>

</p>
</body>
</html>

The user's first name is John
The user's last name is Smith
The user's age is 50

Array Functions

Besides the array() function, PHP includes numerous other functions for use with arrays. The following section describes some of the most common functions. A more extensive list is available at the PHP web site.

count() - the count function is used to count the number of elements in an array.

sort() - the sort function is used to sort the elements of an existing array.

shuffle() - the shuffle function is used to randomize the elements of a given array.

sizeof() - the sizeof function is an alias of the count() function.

array_slice($array_name,offset, length) - the array_slice function is used to extract a chuck of an existing array.$array_name is the name of the array to slice, offset denotes the position where the slice will begin, length indicates the number of elements that will be sliced from the array.

array_merge($array_name, $array_name) - the array_merge function is used to combine or merge two or more existing arrays. The names of the arrays are separated by commas.

The following code shows how each of the array functions are used.

<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A Web Page</title>
</head>
<body>

<p>

<?php 

//Two arrays are created

$numbers = array(50,20,18,30,10,7);
$colors = array('red', 'blue', 'green')

// determines the size of the array $numbers - 6

$array_size = sizeof($numbers);  

// sorts the elements of the $numbers array - returns array(7,10,18,20,30,50)

sort($numbers); 

// randomizes the elements of the $numbers array

shuffle($numbers);

// $merged_array returns array(7,10,18,20,30,50,'red','blue','green')

$merged_array = array_merge($numbers,$colors);

// slice the numbers 18 and 20 from the sorted $numbers array
// array $slice returns array(18,20)

$slice = array_slice($numbers, 2, 2);


?>

</p>
</body>
</html>

PHP also includes a number of pre-defined or global arrays. These are also known as super-global variables because they are always present and available to all PHP script blocks. Following are commonly used PHP superglobal variables:

$_GET[]
$_POST[]
$_REQUEST[]
$_COOKIE[]
$_FILES[]
$_SERVER[]
$_ENV[]
$_SESSION[]

The PHP superglobal variables will be described in later tutorials. Arrays have many uses in PHP and programming in general. This section has introduced some of the basic PHP array topics and described some basic functions. This foundation will be necessary as more advanced array topics are introduced in later sections.