PHP functions - Why PHP functions are useful
The PHP programming language proposes a set of predefined functions for you to accomplish the most simple tasks.
For instance, when you want to print the content of a PHP variable $var, you can use the print() function. Such a task is so elementary that not providing a specific PHP command for it would be ridiculous; conversely, it would be equally nonsensical to provide predefined PHP functions for every possible sequence of PHP commands, since such functions would hardly be used because they are based on very particular web development needs.
As a web developper, you are thus able to define your own functions in PHP, that is to say sequences of PHP statements which can accept parameters and can be called from different places of a script in order to complete a defined set of tasks whenever you need it.
The reasons you can have for defining a PHP function are severalfold:
- There is a sequence of PHP commands that you will need to reuse multiple times in your script(s).
- A sequence of commands that needs to be executed in your PHP script requires a common set of client-defined parameters.
PHP functions - How to define a function in PHP
The PHP syntax to define a function is rather easy to remember:
function function_name($par_1, ... , $par_n)
{
php_instructions
}
- where function_name is the name of your function (a PHP function name must have the same form as a PHP variable name, the only difference being that you must not put a dollar symbol in front of it)
- where the $par_i denote parameters that are accepted by the function (its arguments, in other words)
- where php_instructions is the set of PHP instructions that must be executed by the function.
Remarks:
- Unlike PHP variable names, PHP function names are case insensitive. It is however recommended to remain rigorous when naming and calling any PHP object (remember what we said about how easy it is to make errors in loosely typed programming languages).
- A function can be defined within another function.
- Every function has a global scope (even if it was defined within another function).
PHP functions - Arguments of PHP functions
Passing an argument by value
By default, a PHP function passes arguments by value (this is what is achieved by the above syntax). This means that once the argument is passed to the function, changing its value within the function won't affect its value outside of the function, as illustrated by the example below:
Learn the PHP code:
|
<?php function add_one($number) { $number = $number + 1; } $my_number = 1; add_one($my_number); echo $my_number; ?> |
Run the PHP script in your web browser:
Remark:
You notice that the variable $my_number was not modified by the execution of the function add_one(). This is because the parameter $my_number was passed by value.
Passing an argument by reference:
On the other hand, an argument can be passed by reference. This means that changing the argument within the function will also change the argument outside the function. In order to achieve this, all you have to do is add an ampersand & in front of the parameters that must be passed by argument in the definition of the function:
function function_name(&$par_1, ... , &$par_n)
{
php_instructions
}
We can modify the above example in order to illustrate this fact:
Learn the PHP code:
|
<?php function add_one(&$number) { $number = $number + 1; } $my_number = 1; add_one($my_number); echo $my_number; ?> |
Run the PHP script in your web browser:
PHP functions - Recursive PHP functions
PHP allows the definition of recursive functions, that is to say functions that call themselves within their own definition in order to get evaluated by induction. An example of recursive PHP function is provided below:
Learn the PHP code:
|
<?php function cumulative_sum($number) { $result = 0; if ($number > 0) $result = $number + cumulative_sum($number-1); return ($result); } ?> |
Run the PHP script in your web browser:
Remark:
The return() statement ends the execution of the function and returns the value of the function call (which is taken as an argument of return()).
PHP functions allow you to group a particular set of PHP commands which require a common set of arguments; in this way, a PHP function can be called multiple times within your script, or even be exported to other scripts by using the include() statement (or the require() statement). PHP functions constitute the building blocks of your applications.
Next tutorial: Using HTML forms in PHP
Previous tutorial: PHP include and PHP require
Back to computer forums
