Tip:
Highlight text to annotate it
X
A function is a reusable code block that will execute only when it’s called.
To create one we use the function keyword followed by a name, an optional parameter
list, and a code block. We can then call the function from anywhere
on the page, by typing it name followed by a set of parenthesis.
The function names are case-insensitive, but it's good standard to use the same casing
as they have in their declaration.
The parenthesis after the function name can be used to pass arguments to the function.
To do this we must first add them to the function declaration in the form of a comma separated
list. We can then use the arguments in the function just as any other variable.
Make sure to call the function with the same number of arguments.
We can also specify default values for the arguments by assigning them a value inside
the parameter list. Now if we don't specify that argument when
calling the method the default value will be used instead.
For this to work as expected it's important that the default values are to the right of
the other arguments.
If we call a method with too few arguments we will get an error, but there's nothing
to stop us from passing more arguments than we've defined in the function.
This allows us to pass a variable number of arguments, which can then be accessed using
some built-in functions. To get one argument at a time we can use this
function, which takes a single value - the parameter to be return.
There's also two more functions, one to get the number of arguments passed and another
to get an array containing all the arguments.
An important keyword we can use in functions is return, which will cause the function to
exit. It can also be given a value to return and
will then make the function call evaluate to that value.
By default, function arguments are passed by value. This means that if the value of
the argument is changed it doesn't get changed outside of the function. To allow a function
to modify an argument it must instead be passed by reference.
This is done by adding an ampersand (&) before the arguments name in the function definition.
Normally, a variable in PHP exists throughout the life of that page and can therefore be
used anywhere in the script. However, within functions a local function
scope is now introduced. Any variable used inside a function is by
default limited to this scope, so trying to access a global variable will not work.
Another useful keyword is static, which makes the function remember the variable.
A static variable exists only in a local function scope, but it does not lose its value when
the function ends. We can for example use this to make a counter.
Since the advantage of functions is that they're reusable we often want to use the same functions
on multiple pages. This can be done first placing the function
code inside a separate file and then including it using the include function.
This function takes all the text in the specified file and includes it, just as if the code
had been typed on the current page.