Functions in PHP
As invaluable as PHP's built in functions are, the real power and flexibility comes from being able to write and utilize your own. Imagine you wanted to do something very simple, like print out a greeting. There is no real reason why you would actually need to use a function for something like this, but for the purposes of the next few examples we will. Take a look at the following:
<?php |
At this point, nothing is outputted to the browser because we are only
defining what the function is called, and what it will do.
Let's take a closer
look at what exactly this code means.
The first line begins
with the word "function", which tells PHP that we are starting to write
a function. "Say_hi" is the name of the function. Just like variable names,
function names must be one word, and can only contain letters, numbers,
and underscores. They cannot begin with a number and are case sensitive.
The name is followed by an opening and closing set of parentheses, which
can house optional arguments, and an opening bracket that denotes where
the code within the function begins.
Unlike Control Structures,
the opening and closing brackets on functions are required even if you
only have one line of code between them.
To actually execute this function, it needs to be called, just like PHP's
predefined functions:
<?php |
Keep in mind that since version 4 of PHP, it has not been necessary to declare a function before calling it.
Returning Values from a Function
In the last snippet,
text was ouputted directly from inside the function using print(), but
what if you just wanted to return a value without actually outputting
it? Let's take a look at our example again:
<?php |
Notice how the line within the function has changed: "Hi there" is now
preceded by the return statement. When you call a function that uses the
return statement, rather than executing and immediately outputting whatever
was in the function, the value following "return" is treated
as the result of the function. This resulting value can be a string, integer,
variable or any other PHP data type. When the function is called, the
result can be assigned to a variable, or printed out.
PHP technically only
allows you to return a single value, but there are ways around this limitation
through the use of Arrays, as you will later learn.
Function Arguments
Imagine that we
wanted to make our little script a little more personalized, and greet
someone by their name. To do this, we want to make the function accept
an argument:
<?php |
In the function definition,
we give the argument a variable name. This is the name that the argument
will be accessible as within the function. Calling the function is the
same as before, except this time we include a string between the parentheses
as an argument:
<?php |
PHP allows you to have as many arguments as you wish. To include more
than one, you need to separate them with a comma:
<?php |
In most cases (although not all), you must have a matching number of arguments in the function defination and in the function call.
Page 2 of 3
This article was originally published on January 17, 2002