PHP Function Explanation

Real power of a programming language comes from its functions. More than thousands of built-in functions defined in PHP language proves 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 terms 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 am going to discuss PHP function’s anatomy elaborately.    

PHP function explanation for the beginner
PHP function explanation for the beginner

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 language. 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 and more built-in functions in your PHP program. So, do Google and find your required function or view PHP documentation and then apply that function in your application. Some popular built-in functions used frequently in PHP program 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 the PHP programmer. User defined function does not execute immediately when a PHP page loads. A function is executed when the function is called from anywher of the program.

A user defined function has the following two part.

  1. Function definition
  2. Function calling

PHP User Defined Function Definition

To create a function in PHP language, we have to first declare the function which is known as function definition. A 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 a 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.

<?php
  function add($a,$b){
    $c=$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 call 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 of the function is present, the argument will be mentioned within parentheses.

PHP Function Calling 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. We will now call the add() function to get the function return value.

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

<?php
 function add($a,$b){
   $c=$a+$b;
   return $c;
 }
 $result=add(5,6);
 echo "The result is ".$result;
?>

Output

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 PHP built-in function echo.

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

<?php
 function show(){
   echo "I am called without arguments and I have no return value.";
 }
 show();
?>

Output

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 at function calling. The show() function also has no return value. So, the return keyword has been 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.  

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, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

PHP break, continue and goto Statements

PHP break and continue statements are used to dominate or control PHP loop statements and PHP goto statement is used to jump one execution label to another execution label….

PHP Loop Statements Explanation

Loop statement is one of the most important topics in PHP programming language. Like other programming languages, PHP loop statements are frequently used in PHP programming. So,…

Leave a Reply

Your email address will not be published.

*

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