We’ve used several of the built-in functions, and know where to find many more. Sometimes, however, you’ll want to create your own functions. You will want to do this when you need to do the same thing in several places on your web page. Instead of typing the same code over and over, you can create a function that you can call at each place where you need those things to happen. This way, if you modify the way that thing happens, you only need to modify it in one place.
At the very end of your web page, even after the </html>, add:
<?
//create a select form item based on the array $options
function makeSelect($name, $options) {
$select = "<select name=\"$name\">\n";
$select .= "<option value=\"\">Choose:</option>\n";
FOREACH ($options as $code => $title):
$select .= "<option value=\"$code\">$title</option>\n";
ENDFOREACH;
$select .= "</select>\n";
return $select;
}
?>
You create your own function with the “function” keyword. After the word “function” you need to type the name of your function. After the name of your function, you place parentheses and the names of any arguments your function requires. These arguments will work just like the arguments for the built-in functions. Here, for example, our function “makeSelect” requires two arguments. Wherever we call “makeSelect”, those two arguments will be placed into the containers $name and $options.
The containers within a function are completely separate from any other containers in your page, except for the all-capital underscore containers such as $_REQUEST. Even though our function contains a $name and we already use a $name elsewhere, these two containers are separate and will not interfere with each other. Functions cannot “see” any containers outside of themselves. They can see only what you send them as arguments.
From there, we create a container called $select. We use the “.=” instead of “=” to “append” to our container. The first line puts something into the $select container; the second and subsequent lines append their information to what is already in $select.
Finally, we “return” something to whoever or whatever “called” this function. We return $select, which is the container that contains our <select...> lines.
We can use this function to easily create a <select...> pull-down menu with any name and from any array. Replace the lines that say:
<select name="imagine">
<option value="">Choose:</option>
<?
FOREACH ($pollchoices as $code => $name):
echo '<option value="', $code, '">', $name, '</option>', "\n";
ENDFOREACH;
?>
</select>
with:
<?echo makeSelect("imagine", $pollchoices)?>
You should see exactly what you saw before. But you can now add another array to the beginning of your file:
$movies = array("sahara"=>"Sand Pirates of the Sahara",
"mouse"=>"The Mouse-Trap",
"rose"=>"The Purple Rose of Cairo",
"love"=>"The Crimes of Love"
);
And then you can add, directly below your makeSelect that creates the poll choice,
<br />
What is your favorite movie? <?echo makeSelect("movie", $movies)?>
With a single line, you have added a select for another array. If you decide to change the way that your selects work, you need only change the function to change every select on your page.
Sometimes, you’ll have two uses for what is basically the same function, but each use has a slightly different argument list. For example, suppose that sometimes we want to specify a default choice for our select form items. The function that supports a default selection will be the same as the function that doesn’t support a default extension, with just a little bit of extra PHP code to set the default.
We can combine those two uses into a single function. Change the “function” line of the function to:
function makeSelect($name, $options, $default=false) {
When you create a function with a $container=something, it means that argument is optional. If you don’t supply that argument when you call the function, the container will be set to whatever you put after the equal sign.
In this example, $default will be set to false if we don’t specify a default.
Now, we can break up the <option ...> portion of the function, and add a “ selected” to the option tag if this $code is the same as the $default we’ve sent the function.
<?
//create a select form item based on the array $options
function makeSelect($name, $options, $default=false) {
$select = "<select name=\"$name\">\n";
$select .= "<option value=\"\">Choose:</option>\n";
FOREACH ($options as $code => $title):
$select .= "<option value=\"$code\"";
//if there is a default choice, check to see if this is it
IF ($default):
IF ($default == $code):
$select .= " selected";
ENDIF;
ENDIF;
$select .= ">$title</option>\n";
ENDFOREACH;
$select .= "</select>\n";
return $select;
}
?>
And now you can change “makeSelect("movie", $movies)” to “makeSelect("movie", $movies, $_REQUEST["movie"])”. The movie selection will always default to the previous movie selection.
All optional arguments must be at the end of the argument list. An optional argument before a required argument wouldn’t make sense.