What is Function ?

A function is a reusable piece or block of code that performs a specific action. Its can take input as argument list and return value.In php 800+ functions built in that perform different tasks.

Why use Functions?

Easy Maintenance : Updates to the system only need to be made in one place.
Better Code Organisation : Functions allow us to group blocks of related code that perform a specific task together.
Re-usability : Once defined, a function can be called by a number of scripts in our PHP files. This saves us time of reinventing the wheel when we want to perform some routine tasks such as connecting to the database.

Types of Function

  • User-defined Function
  • Built-In Function

Type of User-Defined Function By Its Properties

  1. Simple User-defined Function
  2. Function with Argument or Parameter
  3. Function With Default Argument or Parameter Value
  4. Call By Reference
  5. Call By Value
  6. Return Function
  7. Recursive Function
  8. Dynamic Function Call

How To Create a Function

1. Simple User-defined Function

  • A function will not execute immediately when a page loads , Until we call it.
  • After a function call program is executed.

Syntax

<?php
function function_name(){
   //Code
   //Process 
}
function_name();
?>

Example

<?php
function message(){
  echo "Hi This is Function Message";
}
message();
?>
//Hi This is Function Message

2. Function with Argument or Parameter

  • We can Pass the data in function through Argument or Parameter Which is separated by comma.

Syntax

<?php
function function_name($value_1,$value_2){
   //$value_1
   //$value_2
   //process
}
function_name(15,5);
?>

Example 

<?php
function sum($value_a,$value_b){
   $total = $value_a+$value_b;
   echo " Total ".$total;
}
sum(15,5);
// Total 20
?>

3. Function With Default Argument or Parameter Value

  • We can set a Parameter or Argument to a default value if the function’s caller doesn’t pass it function works then also.

Syntax

<?php
function function_name($msg=""){
   //process
}
function_name("text");
?>

Example 

<?php
function show_message($msg=""){
   echo "Hello ".$msg;
}
show_message();
show_message("World");
// Hello
// Hello World
?

4. Call By Reference

  • We can pass a variable by reference to a function so the function can modify the variable.
  • we use (&) Ampersand symbol before the argument variables.

Syntax 

<?php
function function_name(&$variable){
   $variable++;
}
$number=5;
function_name($number);
?>

Example

<?php
function adder(&$var){
   $var++;
}
$number=5;
adder($number);
// $number is 6 here
?>

5. Call By Value

  • We can passed value of a Variable directly. It means, if the value of a variable within the function is changed, it doesn’t get changed outside of the function.

Syntax

function function_name($variable){ 
  //process
  //this changes not change outside 
} 
$number = 1; 
increment($number);

Example

function increment($num){ 
   $num = $num + 5;
   echo $num." </br> "; 
} 
$number = 1; 
increment($number);  // 6
echo $number; // 1

6. Return Function

  • A Return Function will return value after execution.
  • Return stops the execution of the function and sends the value back to the calling code.
  • You can return more than one value from a function using return array(1,2,3,4).
  • We can me inline function with return function.

Syntax

<?php
function function_name($number){
   //Process or calculation
   return $number;
}
$number = 1;
echo function_name($number);
?>

Example

<?php
function cube($var){
   return ($var*$var*$var);
}
$number = 5;
echo cube($number); // 125 
$number = 6;
echo " Cube of 6 is ".cube($number); //216
?>

7. Recursive Function

  • A recursive function is a function that calls itself during its execution.
  • This enables the function to repeat itself several times, outputting the result and the end of each iteration.

Syntax

<?php
function function_name(){
   //process
   //condition   
   function_name();
}
function_name();
?>

Example

<?php
function increment($var){
   if($var<=5){ 
      //condition for stop
      echo $var."</br>"; // for display
      increment($var+1); //process $var+1
   }
}
$number = 1;
increment($number);
//1
//2
//3
//4
//5
?>

8. Dynamic Function Call

  • Dynamic Function Call is Store function name in Variable and Call with Variables.

Syntax

<?php
function function_name(){
   //process
   //condition
}
$function = "function_name";
$function();
?>

Example

<?php
function function_name(){
    //process 
    //condition 
    echo "Hello";
     
} 
$function = "function_name"; 
$function();
// Hello
?>
(Visited 230 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

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