User Defined Functions in PHP

In the previous tutorial, we discussed the built-in function. Built-in functions are readymade functions in PHP. Besides the built-in function, you can also create your own functions. The functions that are created by the user are called User Defined functions.

In user defined functions, the user can create the functions according to task perform. While the built-in functions have readymade functionality, you have to use the function. User Defined functions have aslo a lot of benefits.

The developer can be developed as an application by making their own functions. it is a matter of fact that users can create their own functions along with previously created functions.

Definition and use of the User defined functions

User-defined functions are those functions that are created by the user at the present time.

In simple word, the user-defined is made by the programmers according to their needs.

These are not readymade functions like built-in functions.

In PHP there are various built-in function and also within you can create your own functions for different functionality perform .
A function is a block of statements that can be used repeatedly in a program.

A function will execute when the function is called(must be called a function).
Th user define function in PHP declared by the word "function" .


function functionName() {	
    code will be executed;

function – A word to declared the function .
functionName-- A function name (you can use any name for function).

Examples of User defined functions

Creating a user-defined function is very easy. To create a function, use the "Function" word. You have to give the name of the function along with the function word. The name of the function you can take anything. You can create a name of function according to the task. It's a better suggestion to choose the function name.Let's Implement the example of user-defined functions.

In below example we will create a function(user defined ) for display(print ) the names using buit-in function.


function print_name()
echo "Ram.
"; echo "Syam.
"; echo "Mohan.
"; echo "Sohan.
"; } print_name(); ?>

In the above example, we create a print_name() function. In this function, we display simple names with the help of built-in function (echo function). After the creating functionality of a function, we use the function outside the block so that we can use functionality of function.In this example, we call the function outside the function block. By calling the function, we can use function functionalities. We call this function as a user-defined function.

Multiplication by user defined function

As we have discussed above, you can create functions to perform any task.In this User Defined function, we create functionality for multiplication. According to the task, we create the name "Math" of a user-defined function.


function math()
echo "Multiplication of x and y:$z";


Math–-  &nb A function Name .

$x=20; --    Defined value of $x(variable) is 20 .

$y=34;--    Defined value of $y(variable ) is 34 .

$z=$x*$y;--   Multiplication of $x and $y .

Math(); --    Must be called at the outside the function.

For loop with User defined function

In the previous tutorials, we discussed the for Loop in PHP . You can also create a table with the help of the user defined function. The functionality of function can be created by the user-defined function. If you want to create a table with the help of For Loop, then you code under the function. After that, you can easily display the table by calling the function.


function table()
for ($i=0; $i<=10; $i++)
echo $i."
"; } } table(); ?>

In above example we have created a function ,name is table . In the table function, we have used the for loop to create a table and at the end of block we have called the function by it’s name .

Notification functionality by user defined function

In another example, we will create a user defined function for notification .


function notification($msg)
	echo "You have new.$msg";


function notification($msg ) :-

In the above example, we create a function by the name " notification". In the notification function, we pass a variable.In the function block, we display the variable with a message. Out of the function block, we call the function and pass the value of the variable so that the variable does not return the error like undefine variable.These are the biggest benefits of User Defined functions that you can easily create functionalities for any task perform.

Built-In Functions