PHP Function Explanation

Real power of a programming language comes from its functions. More than 1000 built-in functions defined in PHP language prove the power of PHP language. Besides built-in function PHP has capability to declare user defined function like other programming language. So, PHP becomes a dominating programming language having a number of built-in functions and user defined function capability.

A newbie PHP programmer should have enough skill on PHP functions along with other PHP basic items because PHP program frequently uses built-in function as well as user defined function. In my previous article I explained PHP loop statements elaborately and in this article I will discuss details on PHP functions.    

PHP User Defined Function
PHP User Defined Function

PHP Function

A function is a block of statement that takes one or more input values in form of argument and does some processing and then returns a value. PHP function can be divided into the following two categories.

  1. Built-in function
  2. User defined function

PHP Built-in Function

Built-in functions are those functions whose definition is predefined in PHP. More than thousands of built-in functions are present in PHP. Built-in function has predefined arguments and return that we have to know before usage. If you do practice regularly, you will able to use more built-in functions in your PHP program. So, do Google and find your required function and then apply that function in your application. Some popular built-in functions are echo(), printf(), isset(),strcmp(), strlen(), strncmp(), strrev(), is_array(), array_merge(), array_push() etc.

PHP User Defined Function

User defined function is that function whose definition is defined by PHP programmer. User defined function does not execute immediately when a PHP page loads. A function is executed when the function is called from program. So, a user defined function has the following two part.

  1. Function definition
  2. Function calling

PHP Function Definition

To use a user defined function, we have to first declare the function which is known as function definition. PHP user defined function is declared like the following structure.

function function_name(arg1,arg2,…argN){
return return_value;

From the above structure we can see that there are five parts of a user defined function.

  1. function keyword (required).
  2. Function name (required).
  3. Function body enclosed with curly braces (required).
  4. Function arguments within parentheses (optional).
  5. Function return value (optional).

PHP user defined function must start with function keyword and function name separated by a space. Required arguments can be passed through argument part. The function body must start with starting curly brace ({) and end with ending curly brace (}). Optionally, a user defined function may have a return value with return keyword. Return part of a user defined function returns the final result to the caller function.

PHP Function Definition Example

The following program is a user defined function that will add two numeric values and return the result to the caller function.

  function add($a,$b){
    return $c;

The above add() function will not be executed immediately when the PHP page loads. It will be executed after calling the function from anywhere in this page.

PHP Function Calling

After creating a user defined a function, the function must be called to get the estimated result. PHP user defined function is called like the following structure. 

function_name(arg1, arg2,….argN);

From the above structure we can see that to call a user defined function we have to mention the function name with parentheses and if any argument is present, the argument will be mentioned within parentheses.

PHP Functions with Example

In the previous example we have created a user defined function (named add()) that will add two numeric values and return the result to the caller function.  Now we will call the add() function to get the function return value.

The following program is an example of PHP user defined function definition and calling the defined function to get the return value.

 function add($a,$b){
   return $c;
 echo "The result is ".$result;


The result is 11.

In the above program the add() function is being called with the required arguments. After processing the operation, the function is returning the result which is being stored in the $result variable and then the stored value is being showed with the built-in function echo.

If any function does not have any argument and return value, the function should be declared like the following example.

 function show(){
   echo "I am called without arguments and I have no return value.";


I am called without arguments and I have no return value.

Here, the show() function is a user defined function which has no argument. So, no argument has supplied when the function is being called. The show() function also has no return value. So, the return keyword is being ignored here.

PHP function definition and function calling has been explained in this article, I hope you will now be able to define and call a user defined function in PHP program. However, if you face any confusion, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.  

Related post:



I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, JavaScript, AJAX, jQuery, WordPress and CodeIgniter framework. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a iTechSheet author.

One thought on “PHP Function Explanation

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.