In this section we’ll look at arrays, how we use them, why we use them and we’ll take a look at a few special arrays available to you in PHP scripts.
ArraysArrays in PHP are just what they say, arrays of variables. Let’s take a look at what one would look like.
An array is accessed by the name of the array, followed by square brackets containing the key of the array item we want to access.
An array contains an array of values, each of which are referenced by this “key”. We use the keys to access each individual item within the array. We would assign an array like this:
All that information would be stuck into the $array array. We could then access each item:
The key index consists of numbers, the first value in the array is accessed with 0, NOT 1. The second accessed by 1, the third by 2, and so forth.
Associative ArraysYou don’t have to access array items with number values starting at 0, you can define your own keys.
That may look a bit daunting, but just look at it. Each array key => item is defined separated by commas. The part before the => is the key and the part after it is the value associated with that key.
Associative arrays allow you to associate different words with each array item, it is easier to remember which word accesses what item in large array, your mind rememebrs words easier than having to count, starting from zero, in your head.
Remember the single quotes/double quotes thing? This applies when accessing array items with keys also. An integer value or a variable needs not be quoted, a string value needs aingle quotes, a string value containing a variable needs double quotes.
$_SERVERThis array contains lots of values for you to use in your scripts. It contains the client’s IP address, the browser version, the URI of the script being access, various HTTP headers and much more, here are some examples.
$_POSTThis array contains anything that has been submitted through the POST method. We will be looking at this in the next chapter.
$_GETThis array contains anything that has been submitted through the GET method. We will be looking at this in the next chapter.
$_COOKIEContains cookie information for the client. Say you set a cookie on their machine named “mycookie” with a value of “Jester”.
$_REQUESTThis associative array consists of everything in $_POST, $_GET, $_COOKIE and $_FILE.
For more information see The PHP Manual.
In the next chapter we’ll discuss using forms with PHP scripts and how we process the information submitted.
No Comments for this page.