HTML and PHP Resources

Short easy to follow tutorials & more…

Introduction to custom functions in PHP

| 0 comments

Introduction

The PHP language has a few thousand functions built in and ready to use. Although several thousand functions exist it is entirely possible that the a function to accomplish something you are looking for may be too general for your purposes or not exist at all.

Although a large number of functions in PHP may take parameters and do operations on those parameters, functions are also a great way to group commonly used code in order to improve readability and maintainability of a program.

Defining a Function

To define a function we are required to use the keyword, function followed by the function’s name and parenthesis encasing any parameters separated by a comma and a block of code wrapped in curly brackets.

Function definition without parameters: 

PHP
1
function printHello(){ echo 'Hello!'; }

Function definition with one parameter: 

PHP
1
function sayHello($name){ echo 'Hello ', $name, '!'; }

Function Names

Functions can have just about any name as long as they begin with a letter or underscore, the first character can be followed by any letter/number/underscore combination.

Names that are PHP keywords or already existing functions should be avoided. Naming custom functions the same as existing functions will result in error and will keep your script from running.

PHP does not support function overloading or re declaring of functions. Be unique and descriptive when naming your functions.

(Function overloading is simply when you define multiple functions with the same name, differing only in the number of parameters. To set optional parameters in php, you can use default values when you declare your function (see next section.))

Function Parameters

When we declare our custom function we can define the number of parameters that our function can accept. The number of parameters can range from none to as many as you want, they only need to be separated by a comma.

PHP
1
2
3
4
5
6
7
8
// No Parameters
function hello0(){ /*code*/ }
// One Parameter
function hello1($par1){ /*code*/ }
// Two Parameter
function hello2($par1, $par2){ /*code*/ }
// Three Parameter
function hello3($par1, $par2, $par3){ /*code*/ }

 The names that are given to our parameters will be the names we will use inside our function’s block of code. 

PHP
1
2
3
function showName($name) {
echo $name;
}

In the example above, our only parameter was given the identifier of $name and that is what is used inside our function to use the passed parameters value.

It should be noted that is the parameter that was passes is changed inside the function, the original value will remain unchanged.

PHP
1
2
3
4
5
6
7
function changeName($name){
$name = 'James';
}
$user = 'Joe';
changeName($user);
echo $user; // Joe - still

 It should be noted that php functions can have optional parameters (which addresses the issue of function overloading). We have the freedom of setting default values for our functions, these default values are used when no value is passed to our function. Consider the example below:

PHP
1
2
3
4
5
6
7
function printName($name = "No Name"){
echo $name;
}
$user = 'Joe';
printName($user);//echo's Joe
printName(); // echo's No Name

 Note how the parameter has become optional, if we do not specify a parameter, then the default from our function declaration is used. One thing to keep in mind about optional parameters is that no required parameters must come after the optional parameters. That is, you have to make sure any optional parameters are specified after required parameters(parameters with no default specified).

Passing By Reference

In our previous example the value held by our $user variable remained unchanged, this is because the value of the parameter is copied when it is passed to a function. In order for our original variable to be accessible and changeable we need to pass it by reference. Passing by reference simply means that our identifier inside our function will refer to the exact location in memory for the value that is being passed, rather than create a copy of it to work with.

Since $name inside our function refers to the same exact place and value as $user any changes to $name will also apply to $user.

To pass by reference, we simply add a ampersand(&) symbol to our parameter when we declare our function.

PHP
1
2
3
4
5
6
7
function changeName(&$name){
$name = 'James';
}
$user = 'Joe';
changeName($user);
echo $user; // James - name changed!

Return Values

Lastly we arrive at return values. Return values are data that a function send back when it finished running, the functions we have created so far do not return data after execution. To return data we must use the return keyword, followed by the variable name or data we are trying to return. It should be noted that the return keyword marks the end of the function, no code after the return is executed (unless you wrap your return in conditionals.) Consider the example below:

PHP
1
2
3
4
5
6
7
function getName(){
$name = 'James';
return $name;
}
$user = getName();
echo $user; // James

We defined a function getName() which returns the value James. When we call the function the value is returned and we store the result in the variable $user, just as if it had been a string. Had we not returned the value from inside the function, the data would have ceased to exist after the function reached its end. We would not have had a value for our $user variable.

Note that you can return any type of data from a function, strings, integers, arrays, ect.

Summary

You should not be able to define your own custom functions, here we covered mostly the syntax and topics surrounding function, but when combined with your knowledge of other html topics it is a very powerful tool. Inside your functions body, you can do anything you can outside of a function and when it comes to reusing that particular block of code inside your function all you have to do is call your function!

If you have any questions, comments or suggestion feel free to leave a comment or contact me at php@htmlandphp.com.

Leave a Reply