The real power of PHP comes from its functions; it has more than 1000 built-in functions.

PHP User Defined Functions


Besides the built-in PHP functions, we can create our own functions

Create a User Defined Function in PHP

A user defined function declaration starts with the word "function":

Syntax:

    function functionName() {
      code to be executed;
    }

Note: A function name can start with a letter or underscore (not a number).

Tip: Give the function a name that reflects what the function does!

Note : Function names are NOT case-sensitive.

PHP Function Arguments


Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma.

The following example has a function with one argument ($fname).

When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

function familyName($fname) {
   echo "$fname Refsnes";
}

familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");

The output will be:
Jani Refsnes.
Hege Refsnes.
Stale Refsnes.
Kai Jim Refsnes.
Borge Refsnes.

The following example has a function with two arguments ($fname and $year):

function familyName($fname,$year) {
  echo "$fname Refsnes. Born in $year ";
}

familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");

The output will be:
Hege Refsnes. Born in 1975
Stale Refsnes. Born in 1978
Kai Jim Refsnes. Born in 1983

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

function setHeight($minheight=50) {
  echo "The height is : $minheight";
}

setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);

The output will be:
The height is : 350
The height is : 50
The height is : 135
The height is : 80

PHP Functions - Returning values

To let a function return a value, use the return statement: 

function sum($x,$y) {
  $z=$x+$y;
  return $z;
}

echo "5 + 10 = " . sum(5,10);
echo "7 + 13 = " . sum(7,13);
echo "2 + 4 = " . sum(2,4);

The output will be:
5 + 10 = 15
7 + 13 = 20
2 + 4 = 6

Explanation of function return value

Values are returned by using the optional return statement. Any type may be returned, 
including arrays and objects. This causes the function to end its execution immediately 
and pass control back to the line from which it was called. See return for more information.

A function can not return multiple values, but similar results can be obtained by returning an array.

Keep in mind that even if PHP allows you to use "return" in the global scope it is very bad design to do so.

Functions References:


What References Are


References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. See What References Are Not for more information. Instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The closest analogy is with Unix filenames and files - variable names are directory entries, while variable content is the file itself. References can be likened to hardlinking in Unix filesystem.


You can pass a variable by reference to a function so the function can modify the variable.

The syntax is as follows:

function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here

RETURN


Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See return for more information.

Note: If the return is omitted the value NULL will be returned.


If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.


Note: Note that since return is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.

Note: If no parameter is supplied, then the parentheses must be omitted and NULL will be returned. Calling return with parentheses but with no arguments will result in a parse error.

Variable parameter counts


int func_num_args ( )

mixed func_get_arg ( int arg_num)

array func_get_args ( )

The printf() function we examined previously is able to take an arbitrary number of parameters. That is, it could take just one parameter, or five, or fifty, or five hundred - it can take as many as are passed into it by the user. This is known as a variable-length parameter list, and it is automatically implemented in your user functions. For example:

<?php      function some_func($a, $b) {          $j = 1;      }        some_func(1,2,3,4,5,6,7,8);  ?>  

Here the function some_func() is defined to only take two parameters, $a and $b, but we call it with eight parameters and the script should run without a problem. In that example, 1 will be placed into $a, and 2 will be placed into $b, but what happens to the other parameters?

Coming to your rescue are three functions: func_num_args()func_get_arg(), and func_get_args(), of which the first and last take no parameters. To get the number of arguments that were passed into your function, call func_num_args() and read its return value. To get the value of an individual parameter, use func_get_arg() and pass in the parameter number you want to retrieve to have its value returned back to you. Finally, func_get_args() returns an array of the parameters that were passed in. Here's an example:

function some_func($a, $b) {
        for ($i = 0; $i < func_num_args(); ++$i) {
            $param = func_get_arg($i);
            echo "Received parameter $param.\n";
        }
    }

    function some_other_func($a, $b) {
        $param = func_get_args();
        $param = join(", ", $param);
        echo "Received parameters: $param.\n";
    }

    some_func(1,2,3,4,5,6,7,8);
    some_other_func(1,2,3,4,5,6,7,8);
	

Using func_num_args() alone you can easily implement function error checking. You can, for example, start off each of your functions by checking to make sure func_num_args() is what you are expecting, and, if not, exit. Once you add func_get_arg() into the mix, however, you should easily be able to create your own functions that work with any number of parameters.