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:
As you can see, there are our three variables - Apples is at index 0 in the array (signified by "=>"), 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:
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:
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: