<<Return to Week | 1 | 2 | 3 | 4

Passing information between php pages

HTML (and by extension PHP) was designed as "stateless" language. What this means is that any given HTML page is dumb, and knows nothing outside of itself (and its linked files). Every time that you create an HTML page, you must supply explicit instructions for it to do everything that you want it to do.

The purpose of PHP, on the other hand, is to create dynamic content that varies according to interactions from the either the user or from information coming from outside of the web page. In order to accomplish this, PHP needs a mechanism for passing information from one page to the next.

There are three such mechanisms in HTML and PHP: HTML forms, cookies, and PHP session variables.

HTML Forms

HTML forms are the principal mechanism for getting information from the user. Undoubtedly you have seen HTML forms, but may not have recognized them. Here is a common example:

User Name:
Password:

Most of the time if you see radio buttons, check boxes, selection boxes, text entry areas, and buttons to submit information on a web page, they utilize HTML forms. The HTML form stores the information in variables that are passed to the receiving page, and can be interpreted by PHP.

Cookies

Cookies are used to store information on the user's computer. They are most often used for storing information that will be used in subsequent visits to the site. They allow a web site, for example, to know who you are, and/or where you have been on the site, when you return. Cookies can be used as a method of passing information between pages, however they are not completely reliable for this purpose. The acceptance of cookies can easily be blocked by the user, and cookies are easily deleted. We will not be going over setting and retrieving information from cookies today, but more information on how to do this can be found at http://us.php.net/manual/en/features.cookies.php.

PHP Session Variables

PHP allows you to start a "session" and store the values of a variable on the server using a "session ID." These variables work much the same as the "Get" and "Post" variables from an HTML form that we will be going over today. The difference is that they can be maintained over multiple pages and subsequent accesses, whereas once you navigate away from the page that a form variable was sent to using the submit button, PHP forgets all about this variable. We will not be going over starting PHP sessions today, but if you have need of this functionality for your projects, information can be found on the php.net site at http://us.php.net/session.

PHP Arrays

Before going further with passing values between pages, you should know a bit about PHP arrays. Arrays are a "data structure." This simply means that they are a structured method to store and manipulate information. There are lots of PHP array functions to do this. Arrays are the structure used to pass information between PHP pages. (They are also used to store information that is returned from a MySQL database.)

Conceptually speaking, you can think of arrays as a named table of key/value pairs.

The default "key" assigned by PHP is a sequential number starting with 0 and getting larger by 1 as each value is added to the array. This key does not, however, have to be numeric. You can assign your own value to the key, and it can be a number or text as long as it is a unique value in the array.

List Notation

The notation that PHP uses for listing the key/value pairs is [key] => value. Thus if you had PHP list the $ArrayOfColors array, it would look something like this:

Array
(
  [0] => Black
  [1] => Violet
  [2] => Blue
  ...
  [7] => White
)

The print_r function is a PHP built-in function that is used to list arrays.

Bracket Notation

The values associated with particular keys in an array can be accessed using bracket notation. To use this notation, you follow the name of the variable with a bracket inside of which is the key. Thus in the array above, the value of

$ArrayOfColors[3]

is equal to "Green".

Arrays of Arrays

The value in an array's key/value pair can itself be an array.

This is known as a two-dimensional array. Arrays are not limited to 2 dimensions, but in fact can be multi-dimensional. To refer to an element in this array, you would use a notation such as this:

$ArrayOfColors[3][2]

And this would refer to the value "Aqua". Here is a link to an example page, ArrayOfColors.php.

<<Return to Week | 1 | 2 | 3 | 4