ptg16476052
668 LESSON 24: Taking Advantage of the Server
}
$new_array = makeArray('one', 'two');
Your function can also return the result of another function, whether it’s built in or one
you wrote yourself. Here are a couple of examples:
function add($a = 0, $b = 0) {
return $a + $b;
}
function alsoAdd($a = 0, $b = 0) {
return add($a, $b);
}
Processing Forms
You learned how to create forms back in Lesson 12, “Designing Forms,” and although I
explained how to design a form, I didn’t give you a whole lot of information about what
to do with form data once it’s submitted. Now I explain how PHP makes data that has
been submitted available to your PHP scripts.
When a user submits a form, PHP automatically decodes the variables and copies the
values into some built-in variables. Built-in variables are like built-in functions—you
can always count on their being defined when you run a script. The three associated with
form data are $_GET, $_POST, and $_REQUEST. These variables are all associative arrays,
and the names assigned to the form fields on your form are the keys to the arrays.
$_GET contains all the parameters submitted using the GET method (in other words, in the
query string portion of the URL). The $_POST method contains all the parameters submit-
ted via POST in the response body. $_REQUEST contains all the form parameters regardless
of how they were submitted. Unless you have a specific reason to differentiate between
GET and POST, you can use $_REQUEST. Let’s look at a simple example of a form:
<form action="post.php" method="post">
Enter your name: <input type="text" id="yourname" /><br />
<input type="submit" />
</form>
When the user submits the form, the value of the yourname field will be available in
$_POST and $_REQUEST. You could return it to the user like this:
<p>Hello <?= $_REQUEST['yourname'] ?>. Thanks for visiting.</p>