Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1
This one line of code will produce substantial output. In the output you'll find a number of variables
relating to your Web server. You can isolate some of these variables, for example:
echo "$HTTP_USER_AGENT\n";

may produce something like the following:
Mozilla/4.7 (Macintosh; I; PPC)
Such information can be useful in knowing what kind of client you've got, where he or she just came
from (HTTP_REFERER), what script you're running (SCRIPT_FILENAME), what request method was
used (REQUEST_METHOD), what query was sent (QUERY_STRING), if any, and so on. In HTML form
processing in particular, these variables can become very useful.

Variables from HTML Forms


PHP makes life fairly easy when you solicit user input in an HTML form. Forms use the common
name=value structure, and anything declared using name= in HTML, creates a variable of that same name
in PHP with the corresponding value.


For example, look at the following HTML form:
<form action="form_example.php3" method="post">
Enter your name: <input type="text" name="username">
What size do you wish to order? <select name="size">
<option value="S">small
<option value="M">medium
<option value="L">large
</select>
<input type="submit" name="SUBMIT" value="Submit">
</form>
When the PHP script is called in form_example.php3, it will have the variables $username and
$size already assigned, according to the user's input. For example, you can use these values to create
a dynamically-generated output, as in the following:
<?php
echo "You entered your name as $username and size as $size.";
?>
You can even receive a whole array of data from a form. You do this with your HTML SELECT tag,
which you declare as SELECT MULTIPLE, and add brackets ([]) .after the name of the variable:
What colors do you want? <select multiple name="color[]">
<option value="R">red
<option value="G">green
<option value="B">blue
<option value="Y">yellow
</select>
Here, you made color[] into an array by the way you declared it in the HTML form, and you now have
the $color array ready for use in PHP.

Expressions


You set equality in PHP by using a single equals sign (=):


$size = 10;
sets the $size variable to the integer 10.

You can do multiple assignments, such as
$leftcolor = $rightcolor = 'brown';

which sets both variables to the same string.
Free download pdf