PHP User Define Function

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP User Defined Function


In all programming and scripting language, a function is a block of statements that can be used repeatedly in a program. In PHP, the concept of the function is the same as in another language like 'C'. There are more than 1,000 in-built functions into the standard PHP distribution. Besides these, we can define functions as per our requirements. These are called 'User Defined Function'.


function function-name()


    statement 1 :

    statement 2 :

    statement 3 :



Elements of a function

function : A function declaration starts with the special word 'function'.

Name of the  function :

The function name is defined by the user.

A valid function name starts with a letter or  underscore, followed by any number of letters, numbers, or underscores.

Remember that function names are case-insensitive.

Opening and Closing curly braces ({  } )

The function body enclosed within a pair of braces which may contain variable names an actual function code. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly ( } ) brace indicates the termination of the function.

Example: PHP function

  1. function myfunction()  

  2. {  

  3. echo "Good Morning";  

  4. }  

  5. myfunction();  

  6. ?>  

When we call the above function it will print Good Morning

View this example in browser

Example : Functions within functions

  1. function function1()  

  2. {  

  3. function function2()  

  4. {  

  5. echo "Good Morning 

  6. }  

  7. }  

  8. function1();  

  9. function2();  

  10. ?>  

In the above example a function, function1() is declared and another function function2() is declared within function1(). Now execute function1() first and then function2() which print "Good Morning", executing function1() makes funtion2() accessible. Therefore we can not call function2() independently without calling function1().

View this example in browser

PHP Function arguments, Returning Values

Function arguments

In PHP information are passed to functions through argument list, which is a comma-delimited list of expression. There are three different ways of passing arguments to a function, arguments by value (the default), passing by reference, and default argument values.

Passing arguments by value

The argument can be any valid expression. The expression is evaluated and its value assigned to the appropriate variable in the function. In the following function $a assigned the value 10 and $b assigned the value 20:

function add($a, $b)
add(10, 20);

Passing arguments by reference

By default, the function arguments are passed by value. If you want to allow a function to change its arguments, you must pass the arguments by reference. To pass an argument to a function as a reference, simply add an ampersand (&) character before the variable name.

  1. function cube(&$x)  

  2. {  

  3. $x = $x * $x * $x;  

  4. }  

  5. $result = 5;  

  6. cube($result);  

  7. echo $result;  

  8. ?>  

Output :


Default arguments values

In the following example the function wage() use a default parameter. When we have called the function without arguments it takes the default value as an argument.

  1. function wage($minwage= 100)  

  2. {  

  3. echo "The wage is : $minwage 
    " ;  

  4. }  

  5. wage(200);  

  6. wage();  

  7. wage(100);  

  8. ?>  

Output :

The wage is : 200
The wage is : 100
The wage is : 100

View this example in browser

PHP: Returning Values

In PHP values are returned by a return statement. The return values must be specified in the variable. If the statement is called within a function the function is terminated immediately and pass control back to the previous position from which it was called.

The return statement can return any type of data.

Example :

In the following example, a function accepts a number and calculate the cube of that number using the return statement.

  1. function cube($x)  

  2. {  

  3. return $x * $x * $x;  

  4. }  

  5. echo "The cube of 4 is : ".cube(4)."

  6. echo "The cube of 9 is : ".cube(9)."

  7. echo "The cube of 20 is : ".cube(20)."

  8. ?>  

Output :

The cube of 4 is : 64
The cube of 9 is : 729
The cube of 20 is : 8000