PHP Function Basics

The real power of a programming language comes from its functions. More than 1000 built-in functions defined in PHP prove the power of PHP language. So, this article is designed to explain PHP function from very beginning.

PHP Function Basics

PHP Function Basics

A function is a block of statement which takes one or more input value in form of argument and does some processing and then return a value. PHP function can be divided into two categories. These are…

  1. Built-in function
  2. User defined function

Built-in Function in PHP

Built-in functions are those functions whose definition is predefined in PHP. More than 1000 built-in functions are present in PHP. You have already seen many built-in functions like echo(), printf() and so on. 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.

User Defined Function in PHP

User defined functions are those functions 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 your program. So, a user defined function has two parts…

  1. Creating a PHP function
  2. Calling a PHP function

Creating a PHP Function

It is easy to create a PHP function. The structure of a user defined function is given below…

function function_name(arg1,arg2,…argN){

return return_value;

}

The structure shows that there are five parts of a user defined function. These are…

  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 function declaration must start with function keyword and then the function name. 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 returns the final result of a user defined function to the caller function.

A user defined function that will add two numeric values and return the result to the caller function is given below…


<?php
  function add($a,$b){
    $c=$a+$b;
    return $c;
  }
?>

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

Calling a PHP Function

After creating a function, the function must be called  to get the estimated result. The structure to call a function is given below…

function_name(arg1, arg2,….argN);

An example of calling a created function is given below…


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

Output: The result is 11.

Here, 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.

If any function does not have any argument and return value, the function should be declared like below 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 has no argument. So, no argument is supplied when the function is being called. Also the show() function has no return value. So, the return keyword is being ignored here.

How to declare a function and how to call a function are explained in this article from very beginning. If you face any problem to understand PHP function, feel free to contact with me from Contact us page. I’ll try my best to stay with you. Some advance topics about PHP function will be explained in next tutorial. Spend some times to study that article and improve your idea about PHP function.

No Responses

Leave a Comment

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