What is a Function?
A function is just a segment of code, separate from the rest of your code. You separate it because it's nice and handy, and you want to use it not once but over and over. It's a chunk of code that you think is useful, and want to use again. Functions save you from writing the code over and over. Here's an example.Suppose you need to check text from a textbox. You want to trim any blank spaces from the left and right of the text that the user entered. So if they entered this:
" Bill Gates
"
You want to turn it into this:
"Bill Gates"
But you also want to check if the user entered any text at all. You don't want
the textbox to be completely blank!You can use the PHP inbuilt function called trim( ). Like this:
$user_text = trim( $_POST['text1'] );
That will get rid of the white space in the text box. But it won't check if
the text box is blank. You can add an if statement for that:
if ($user_text == "") {
error_message = "Blank textbox detected";
}
But what if you have lots of textboxes on your form? You'd have to have lots
of if statements, and check each single variable for a blank string. That's
a lot of code to write!Rather than do that, you can create a single function, with one if statement that can be used for each blank string you need to check. Using a function means there's less code for you to write. And it's more efficient. We'll see how to write a function for the above scenario in a moment. But first, here's the basic syntax for a function.
function function_name() {
}
So you start by typing the word function. You then need to come up with
a name for your function. You can call almost anything you like. It's just like
a variable name. Next, you type two round brackets ( ). Finally, you need the
two curly brackets as well { }. Whatever you function does goes between the
curly brackets. Here's a simple example that just print something out:
function display_error_message() {
print "Error Detetceted";
}
In the example above, we've started with function. We've then called this particular
function display_error_message. In between the curly brackets, there
a print statement. Try it out with this script:
<?PHP
function display_error_message( ) {
print "Error Detetceted";
}
?>
Run your script and see what happens. You should find that nothing happens!The reason that nothing happened is because a function is a separate piece of code. It doesn't run until you tell it to. Just loading the script won't work. It's like those inbuilt functions you used, such as trim. You can't use trim( ) unless you type out the name, and what you want PHP to trim. The same applies to your own functions – you have to "tell" PHP that you want to use a function that you wrote. You do this by simply typing out the name of your function. This is known as "calling" a function. Try this new version of the script.
<?PHP
function display_error_message() {
print "Error Detetceted";
}
display_error_message( );
?>
After the function, we've typed out the name again. This is enough to tell
PHP to run our code segment. Now change your code to this, and see what happens:
<?PHP
display_error_message();
function display_error_message() {
print "Error Detetceted";
}
?>
If you have PHP 4 or above, you should see no difference – the function
will still get executed with the name above or below the function. But for neatness
and readability's sake, it's better to put all of your function either at the
top or bottom of your scripts. Or better yet, in a separate PHP file. You can
then use another inbuilt function called "Include" (which we'll get
to soon)
0 comments:
Post a Comment