Although PHP has a lot of built in functions. Developers often need to write their own functions in order to achieve their objectives. Functions are useful for eliminating repetition, but they can also be used to perform complex tasks and can even be used to group together a series of functions to output a particular result.
Syntax
function name(arguments) {
// code to be executed
}
- The keyword
function
tells PHP that the code following it will be a user defined or custom function. name
is the name of the function and is not case sensitive. A function name can contain numbers, letter, underscores or dashes.- The arguments, also known as parameters are optional inputs that allow you to feed information into the function.
- Finally inside the curly braces
{}
you have your block of code to be executed.
Take a look at the following function:
<?php // our user defined function function person($name) { echo "<p>Name: $name</p>"; } // parameters to pass to the function $name = "Matthew"; // calling our function person($name); // Name: Matthew ?>
The function called person
has a single argument $name
. When calling this function we pass the argument and the function will echo
out the code block.
Sometimes we need to return the output of a function rather than echo
or print
it and to do that we use the return
keyword.
The difference between return
and echo
or print
is that return does not display the ouput. That makes it useful for helper functions that process information and return a certain output.
<?php // our user defined function function person($name) { return "<p>Name: $name</p>"; } // parameters to pass to the function $name = "Matthew"; // calling our function person($name); // returns <p>Name: Matthew</p> ?>
In order to see what a function returns you can always assign the function to a variable and use var_dump()
;
<?php // calling our function $var = person($name); // returns <p>Name: Matthew</p> var_dump($var); ?>
In our console that would display:
string(20) "<p>Name: Matthew</p>"
Arguments and Parameters
In our example above we added an argument that allows a function to take some kind of input. A function can accept as many arguments as you need but you should try to keep your functions simple and easy to understand.
<?php // our user defined function function person($fname, $lname) { echo "<p>First name: $fname</p>n". "<p>Last name: $lname</p>n"; } ?>
Here we have a function that accepts more than one parameter, $fname
and $lname
.
$a
, $b
, $c
. Other developers will frown upon cryptic nonsensical functions.If we tried to call person()
without inputting all of the parameters it will return a notice for each undefined variable similar to this:
What this tells us is that we have not provided the function with the required arguments and therefor the variable is undefined. i.e. it doesn’t exist!
To overcome this issue we can set some default values for each variable or set them to false.
<?php // our user defined function function person($fname = false, $lname = false) { echo "<p>First name: $fname</p>n". "<p>Last name: $lname</p>n"; } person(); ?>
Now when we call this function it will echo
everything in the code block minus the actual values (because we didn’t define any) like this:
First name:
Last name:
Once we pass some arguments.
person("Matthew", "Horne");
We will get the desired outcome like so:
First name: Matthew
Last name: Horne
Avoiding Pointless Output
As mentioned above, when we don’t provide any information to a function, it will still output the rest of the code block. This is not desirable because nothing should output if it has no value.
To overcome this problem we can use an if statement that acts like a gatekeeper:
<?php // our user defined function function person($fname = false, $lname = false) { // gatekeeper if(empty($fname) && empty($lname)) return; // output echo "<p>First name: $fname</p>n" . "<p>Last name: $lname</p>n"; } person(); ?>
Now when we try to call the function person()
without any parameters it will not echo
any output, instead it will return
NULL.
Function-ception
Functions can be called within functions and this is where functions that return
their output are most useful. Functions that echo
output are usually intended to be visible in the browser whereas as functions that return
their output are usually intended to pass their output for further use.
<?php // our user defined function function person($fname = false, $lname = false) { // gatekeeper if(empty($fname) && empty($lname)) return; // output echo "<p>Welcome " . format_name($fname). " " . format_name($lname) . ".</p>n"; } // helper function that returns its output function format_name($string) { // note the order return ucfirst(strtolower($string)); } person("mattHew", "hoRNe"); ?>
In this example we have two functions. The first function person()
provides us with the output that will be visible to the user. The other function format_name()
is a helper function that return
s its output after processing.
Notice that the helper function uses two built in PHP functions; ucfirst()
and strtolower()
which help to correct formatting errors when a user enters their name.
ucfirst()
is a function that converts the first character in a string to uppercase.The order of these two function is important; if the order was:
strtolower(ucfirst($string));
The the output would first convert the first character to uppercase and then convert the whole string to lowercase which is not what we want. So ensure you take note of the order in which functions are called. A rule of thumb would be start from the inner most function and work your way out.
Summarize
By now you should have a good overview of the fundamentals of PHP. It is important that you can properly comprehend the basics as it will lay down a solid foundation for learning some of the more advanced aspects of PHP.