PHP has in-built functions and also allows to create user-defined functions. Following are the advantages of using functions.

  1. Use function wherever required.
  2. No repetition of code.
  3. No code duplicacy.
  4. Error finding and debugging become easier.
  5. Code maintenance become easier.

In PHP function names are case insensitive.

In PHP, Function has two parts.

  1. Function definition
  2. Function call
Function definitionIt is function body which contains function name, arguments and set of statement to execute.
Function callIt invokes (call) the function to execute. without function call, function does not execute automatically.

Create Function PHP

To create a function in PHP, function keyword is used and function name with function symbol () then function body {} (curly braces) which contains statements, which executes when function is called.

Syntax

function function_name(arguments){
 // set of statements
}

Example

<?php
   // function definition/declaration
  function myFunction(){
       echo "Hello from myFunction";
  }
?>

Call a function

To use a function, it needs to be called, and this is called as function call in PHP.

<?php
   
  function myFunction(){
         echo "Hello from myFunction";
  }
 // function call
  myFunction(); 
?>

To call a function, use function name and function symbol without space. If function has parameters, then pass the parameters.

function namemyFunction
function symbol()

Function with parameters

PHP allows to use parameters or arguments with function. These parameters allows to send values to a function, during function call.

<?php
   
  function add($a, $b){
        $c = $a + $b;
         echo $c;
  }
  add(20, 30); // function call
?>

Above function has two parameters $a, $b.

During function call value 20 and 30 is sent, so, 20 is passed to $a and 30 is passed to $b, so the value of $a is 20 and $b is 30.

Default function parameters

PHP allows to set default values to function parameters.

<?php
   
  function add($a, $b=20){
        $c = $a + $b;
         echo $c;
  }
  add(10); // function call
?>

$b= 20, value assigned in function declaration, such values are called default values, It is optional. For such parameters if value is not passed during function call, function uses default value.

Output : 30
<?php
   
  function add($a=10, $b=20){
        $c = $a + $b;
         echo $c;
  }
  add(); // function call
?>

Both $a and $b has default value set, so no need to pass value during function call.

Passing values during function call with default parameters

<?php
   
  function add($a=10, $b=20){
        $c = $a + $b;
         echo $c;
  }
  add(60, 70); // function call

?>
  1. $a has its default value 10, and $b = 20 as its default value.
  2. During function call 60 and 70 is passed to $a and $b respectively.
  3. In such case default values are overridden (replaced) by function call values and $a value is 60 and $b value is 70

Function return values

function are of two type on return value basis.

  1. Function return value
  2. Function no return value.

Function with return value

To return a value from function return keyword is used, It is always used at the end of the function before closing brace.

Syntax

function function_name(arguments){
 // set of statements
 return value;
}

return should be the last statement inside a function.

<?php
function add($a, $b){
  $c = $a + $b;
  return $c;
}
$d = add(20,30);
echo $d;

echo add(30,40);
?>

function add() returns a value to use this returned value, there are two ways

  • Assign the returned value to a variable and output it or use elsewhere.
$d = add(20,30);
echo $d;
  • Use directly
echo add(30,40);

Passing function Arguments by Reference

PHP by default passes arguments by value, but also allows to pass arguments by reference

by valuechange in variable doesnot change argument value, copy of value is used, so variable value cannot be changed
by referenceHere reference means variable address, so, any change in variable value changes the argument value also. Hence the original value gets changed. To use argument as reference & operator is used.

Pass by value example

<?php
function passByValue($a){
  $a = $a + 5;
  return $a;
}
passByValue($n);
?>

Pass by Reference example

<?php
function passByReference(&$a){
  $a = $a + 5;
  return $a;
}
$n = 30;
echo  $n;
passByReference($n);
echo  $n;

?>

Variable Number of Arguments

PHP allows to pass multiple arguments to function using operator. This must be prepended to argument.

<?php

 function printAllnumbers(...$a){
   
  for($i = 0; $i < count($a); $i++){
      
     echo $a[$i];
 }
printAllnumbers(20, 1, 44, 5, 12, 45, 30);
 }

?>