First steps

array array ( [mixed ...])

int count ( mixed var [, int mode])

bool print_r ( mixed expression [, bool return])

void var_dump ( mixed expression [, mixed expression [, mixed ...]])

mixed var_export ( mixed expression [, bool return])

PHP has built-in support for arrays of data, and there are two ways you can create an array: using the array function, or using a special operator, [ ].

Before we look at how arrays work, there are two key things you need to understand before you continue:

  • An array is a normal PHP variable like any others, but it works like a container - you can put other variables inside it

  • Each variable inside an array is called an element . Each element has a key and a value , which can be any other variable.

Now, consider this PHP code:

    $myarray = array("Apples", "Oranges", "Pears");
    $size = count($myarray);

On line one we see the most basic way to create an array, the array() function. The array() function takes a minimum of one parameter (and a maximum of as many as you want), and returns an array containing those variables. That's right - $myarray now contains all three variables. Line two contains a new function, count(), that returns the number of elements consisting in the array passed to it as its only parameter - in the example we pass in my $myarray, then store the number of elements (three) in $size.

Author's Note: The array() function actually allows you to have a trailing comma after the last element, eg: array("Apples", "Oranges", "Pears",). This is rarely used in hand-written code, but it can make generating code much easier.

Line three contains another new function, print_r(). This takes just one parameter, but it outputs detailed information about a variable, such as it is type, length, and contents. In the case of arrays, print_r() iteratively outputs all elements inside the array - it is a good way to see how arrays work.

Here is the output of print_r() from the above code:

[0] => Apples
[1] => Oranges
[2] => Pears

As you can see, there are our three variables - Apples is at index 0 in the array (signified by "[0]=>"), Oranges is at index 1 in the array, and Pears is at index 2 in the array. Note that if you are running your scripts through a web browser as opposed to from the command-line, you may find it help to put a HTML <pre> tag before your print_r() calls, as this will format them for easier reading.

Using the proper array terminology defined earlier, the 0, 1, and 2 indices are the keys of each element, the "Apples", "Oranges", and "Pears" are the values of each element, and the key and the value considered together are the elements themselves.

Note that you can provide a second parameter to print_r(), which, if set to true, will make print_r() pass its output back as its return value, and not print anything out. To achieve the same output using this method, we would need to alter the script to this:

    $myarray = array("Apples", "Oranges", "Pears");
    $size = count($myarray);
    $output = print_r($myarray, true);
    print $output;

You can store whatever you like as values in an array, and you can mix values also. For example: array("Foo", 1, 9.995, "bar", $somevar). You can also put arrays inside arrays, but we will be getting on to that later.

There is a similar function to print_r(), which is var_dump(). It does largely the same thing, but a) prints out sizes of variables, b) does not print out non-public data in objects, and c) does not have the option to pass a second parameter to return its output. For example, altering the first script to use var_dump() rather than print_r() would give the following output:

array(3) {
    string(6) "Apples"
    string(7) "Oranges"
    string(5) "Pears"

In there you can see var_dump() has told us that the array has three values, and also prints out the lengths of each of the strings. For teaching purposes, var_dump() is better as it shows the variable sizes, however you will probably want to use print_r() in your own work.

Finally, there is the function var_export(), which is similar to both var_dump() and print_r(). The key difference with var_export(), however, is that it prints out variable information in a style that can be used as PHP code. For example, if we had use var_export() instead of print_r() in the test script, it would have output the following:

array (
    0 => 'Apples',
    1 => 'Oranges',
    2 => 'Pears',

Note there is an extra comma after the last element, however this is ignored by PHP and you can copy and paste that information directly into your own scripts, like this:

    $foo = array (
        0 => 'Apples',
        1 => 'Oranges',
        2 => 'Pears',

Short array syntax

If you're using PHP 5.4 or later (which is highly probable), you can take advantage of short array syntax: rather than using array(), you can just use square brackets. So, these two lines of code produce identical results:

    $myarray = array("Apples", "Oranges", "Pears");
    $myarray = ["Apples", "Oranges", "Pears"];


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Associative arrays >>

Previous chapter: Arrays

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.