PHP Tutorial – 09 – Functions
A function is a reusable code block that will execute only when it’s called.
Defining functions
To create a function the function keyword is used followed by a name, a set of parentheses with an optional parameter list, and a code block.
function myFunc() { echo "Hello World"; }
Calling functions
The function can then be called from anywhere on the page, by typing its name followed by a set of parenthesis. Function names are case-insensitive, but it’s good practice to use the same casing as they have in their declaration.
myFunc(); // Hello World
Parameters
The parenthesis after the function name can be used to pass arguments. To do this first add the desired number of parameters to the function declaration in the form of a comma separated list. These arguments can be used in the function just as any other variables.
function myFunc($x,$y) { echo $x . $y; }
With the parameters specified in the declaration the function can now be called with the same number of arguments.
myFunc("Hello", " World"); // Hello World
Default parameters
It’s possible to specify default values for parameters by assigning them a value inside of the parameter list. Then, if that argument is unspecified when the method is called the default value will be used instead. For this to work as expected it’s important that the parameters with default values are to the right of the ones without default values.
function myFunc($x,$y = " Earth") { echo $x . $y; } myFunc("Hello"); // Hello Earth
Variable parameter list
A method cannot be called with fewer arguments than is specified in its declaration, but it may be called with more arguments. This allows us to pass a variable number of arguments, which can then be accessed using a couple of built-in functions. First, for getting one argument at a time there is the func_get_arg method. This method takes a single argument; the parameter to be returned starting with zero from the left.
function myFunc() { $x = func_get_arg(0); // get specified argument $y = func_get_arg(1); $z = func_get_arg(2); echo $x . $y . $z; } myFunc("Hello", "Hi", "Hey"); // HelloHiHey
There are two more functions related to the argument list: func_num_args gets the number of arguments passed and func_get_args gets an array containing all of the arguments.
function myFunc() { $num = func_num_args(); // get number of arguments $args = func_get_args(); // get array with arguments for ($i = 0; $i < $num; $i++) echo $args[$i] . " "; } myFunc("Hello", "Hi", "Hey"); // Hello Hi Hey
Return statement
An important keyword in functions is return, which causes the function to exit.
function myFunc() { return; // exit function }
It can optionally be given a value to return, in which case it will make the function call evaluate to that value.
function myFunc() { return "Hello"; // exit function and return value } echo myFunc(); // Hello
Passing by 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.
$x = "Hello"; myFunc($x); // value of x is passed echo $x; // Hello function myFunc($x) { $x .= " World"; }
Passing by reference
To allow a function to modify an argument it must be passed by reference. This is done by adding an ampersand before the argument’s name in the function definition.
$x = "Hello"; myFunc($x); // reference to x is passed echo $x; // Hello World function myFunc(&$x) { $x .= " World"; }
Scope and lifetime
Normally, a PHP variable’s scope starts where it’s declared and lasts until the end of the page. However, within functions a local function scope is introduced. Any variable used inside a function is by default limited to this scope, so trying to access a global variable will not work and will instead create a new local variable. In order to make a global variable accessible the scope of that variable must be extended to the method by redeclaring it with the global keyword.
$x = "Hello"; // global x function myFunc() { global $x; // set x as global in this function $x .= " World"; // changes global x } myFunc(); echo $x; // Hello World
Static variables
Another useful keyword is static, which causes the function to remember the variable. A static variable exists only in the local function’s scope, but it does not lose its value when the function ends. This can for example be used to make a counter of the number of times a function has been called.
function myInc() { // Make x keep its value (only called once) static $x = 0; // Increment x echo $x++; } myInc(); // 0 myInc(); // 1 myInc(); // 2
Include
Since the main advantage of functions is that they’re reusable the same functions often need to be called on multiple pages. This can be done by first placing the function code inside a separate file and then including that file using the include function. This function takes all the text in the specified file and includes it into the page, just as if the code had been copied to that location.
include("my.php");
Method overloading
Unlike many other languages, PHP does not allow a method to be defined multiple times with different arguments. This technique, called method overloading, has the benefit of allowing a method to handle a variety of parameters transparently to the user.
It is however possible to achieve the benefits of method overloading in PHP. Since PHP does not have strong typing, a function parameter already accepts any data type. This loose typing, along with default parameter values and variable parameter lists, makes it possible to duplicate the effect of method overloading.
// Prints a variable number of parameters function myprint() { $a = func_get_args(); foreach ($a as $v) { echo $v; } } myprint(1,2,3); // 123
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

