Enter your email id to get regular update free
Login
Your Email
Pasword
Home
Write
Trending
History
Liked
Earning

How to Create and Call PHP Functions

Function creation and calling is an integral part of any programming language. Working with PHP functions are similar to other programming language. A function is a piece of code that takes one or more parameters as input, processes them and returns some value to the calling function. There are some inbuilt PHP functions but PHP gives options to create user defined functions as well.

   Function creation and calling is an integral part of any programming language. Working with PHP functions are similar to other programming language. A function is a piece of code that takes one or more parameters as input, processes them and returns some value to the calling function. There are some inbuilt PHP functions but PHP gives options to create user defined functions as well. There are two parts in PHP functions, 
• Creating a PHP Function 
• Calling a PHP Function 
   PHP provides more than 1000 built-in library functions created for different requirements of the user. Creating PHP functions: It is very easy to create a user defined PHP function. To create a PHP function its name should start with the keyword function and all the code of the function should be inside opening and closing curly braces “{ }”. Below is a PHP function named “message()” which is created and called. 
Example:  
Output: 
                                     Hello World, Good Morning! 
PHP functions with Parameters: 
    PHP gives option to pass parameters inside a function like any other programming language. One can pass multiple numbers of parameters to a PHP function. The below example takes two integers as parameter to a function, add them together and prints the result. 
Example:  
Output: 
                                Sum of the two numbers is : 70 
PHP parameter passing by reference: 
    PHP also allows users to pass arguments to functions by reference. The value of the original variable will be changed if any changes are made to an argument. To pass arguments as reference add an ampersand to the variable name in either the function call or the function definition. 
Example: 
Output: 
                        Original Value is 10 Original Value is 16 
PHP functions returning some value: 
    Like any other programming language PHP also can return a value using the return statement. The return statement stops the execution of the function and returns the value and control to the calling function. The below example takes two integer as parameters, add them and return the sum of the two number to the calling function. 
Example:  
Output: 
                          Returned value from the function : 30 
 Using Default values for function parameter: 
    In PHP one can set arguments or parameters to have default value if the function called does not pass any parameter or argument. Below is an example of a function that prints NULL if no parameter or argument is passed. 
Example:
Output:
                                                      This is test 
                                                      NULL 
Summary: 
1. In spite of built-in library function there are also options for user defines function in PHP. 
2. To define a function in PHP the keyword “function” is used. 
3. We can pass multiple number of parameter or argument to a PHP functions. 
4. Arguments can be passed to a PHP function by reference. 
5. The “return” statement is used to return a value or control to the calling function. 
6. PHP also allows to pass default argument values to a function.  
PHP function
Tutorial on PHP functions
Creating PHP function
Calling PHP function
Types of PHP function
Education
Samiran Panda
Lecturer
Total 319 views