PHP Manual
Prev | Chapter 7. Variables | Next

# Variables from outside PHP

## HTML Forms (GET and POST)

When a form is submitted to a PHP script, any variables from that form will be automatically made available to the script by PHP. For instance, consider the following form:

Example 7-1. Simple form variable

      1
      2 <form action="foo.php3" method="post">
      3     Name: <input type="text" name="name"><br>
      4     <input type="submit">
      5 </form>
      6

When submitted, PHP will create the variable `$name`, which will will contain whatever what entered into the Name: field on the form.

PHP also understands arrays in the context of form variables, but only in one dimension. You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input:

Example 7-2. More complex form variables

      1
      2 <form action="array.php" method="post">
      3     Name: <input type="text" name="personal[name]"><br>
      4     Email: <input type="text" name="personal[email]"><br>
      5     Beer: <br>
      6     <select multiple name="beer[]">
      7         <option value="warthog">Warthog
      8         <option value="guinness">Guinness
      9         <option value="stuttgarter">Stuttgarter Schwabenbräu
     10         </select>
     11     <input type="submit">
     12 </form>
     13

If PHP's track_vars feature is turned on, either by the track_vars configuration setting or the `<?php_track_vars?>` directive, then variables submitted via the POST or GET methods will also be found in the global associative arrays $HTTP_POST_VARS and $HTTP_GET_VARS as appropriate.

### IMAGE SUBMIT variable names

When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:

      1
      2 <input type=image src="image.gif" name="sub">
      3

When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables, sub_x and sub_y. These contain the coordinates of the user click within the image. The experienced may note that the actual variable names sent by the browser contains a period rather than an underscore, but PHP converts the period to an underscore automatically.

## HTTP Cookies

PHP transparently supports HTTP cookies as defined by Netscape's Spec. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the SetCookie() function. Cookies are part of the HTTP header, so the SetCookie function must be called before any output is sent to the browser. This is the same restriction as for the Header() function. Any cookies sent to you from the client will automatically be turned into a PHP variable just like GET and POST method data.

If you wish to assign multiple values to a single cookie, just add [] to the cookie name. For example:

      1
      2 SetCookie ("MyCookie[]", "Testing", time()+3600);
      3

Note that a cookie will replace a previous cookie by the same name in your browser unless the path or domain is different. So, for a shopping cart application you may want to keep a counter and pass this along. i.e.

Example 7-3. SetCookie Example

      1
      2 $Count++;
      3 SetCookie ("Count", $Count, time()+3600);
      4 SetCookie ("Cart[$Count]", $item, time()+3600);
      5

## Environment variables

PHP automatically makes environment variables available as normal PHP variables.

      1
      2 echo $HOME;  /* Shows the HOME environment variable, if set. */
      3

Since information coming in via GET, POST and Cookie mechanisms also automatically create PHP variables, it is sometimes best to explicitly read a variable from the environment in order to make sure that you are getting the right version. The getenv() function can be used for this. You can also set an environment variable with the putenv() function.

## Dots in incoming variable names

Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it:

      1
      2 $varname.ext;  /* invalid variable name */
      3

Now, what the parser sees is a variable named $varname, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result.

For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.

## Determining variable types

Because PHP determines the types of variables and converts them (generally) as needed, it is not always obvious what type a given variable is at any one time. PHP includes several functions which find out what type a variable is. They are gettype(), is_long(), is_double(), is_string(), is_array(), and is_object().

Prev | Home | Next
Variable variables | Up | Constants