PHP - Functions


Advertisements

Previous Page Next Page

A function is a block of statements which executes only when it is called somewhere in the program. A function provides re-usability of same code for different inputs, hence saves time and resources. There are some in-built functions in PHP and one of the common function is echo() which is used to print the output. Users can also create their own function which is also termed as user-defined functions.

Create Function

In PHP, creating a function starts with keyword function followed by function's name and parenthesis containing function's parameter(s), if it has any. At last, it contains block of statements which is also called body of the function. Please see the syntax below:

Syntax


//Defining function
function function_name(parameters)
  {
    statements;
  }

Example: A function with no parameter

In the below example, a function called MyFunction is created to print Hello World!. The function requires no parameters to execute.

<?php 
  function MyFunction(){
      echo "Hello World!.\n"; 
  }

  MyFunction();
?>

Output

Hello World!.

Call Function

After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any and semicolon (;). As in the above example, the function is called using the following statement:

MyFunction();

Parameter

A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.

Example: A function with parameters

In the below example, the function called MyFunction is created which requires two integer numbers as parameters and print sum of two numbers in desired style.

<?php 
  function MyFunction($x, $y){
    $z = $x + $y;
    echo "Sum of $x and $y is: $z.\n"; 
  }
  
  $a = 15; 
  $b = 10;
  MyFunction($a, $b);
?>

Output

Sum of 15 and 10 is: 25

Function to Return Values

A function can also be used to return values. In the below example, the function called MyFunction returns sum of two digits.

<?php 
  function MyFunction($x, $y){
    $z = $x + $y;
    return $z; 
  }
  
  $a = 15; 
  $b = 10;
  echo MyFunction($a, $b);
?>

Output

25

Default Parameter Value

Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses default value.

Example:

In the below example, default parameter value is used to perform sum operation on two, three and four variables using same function called MyFunction. The default value is set while defining the function. When the function is called without parameter, then it uses parameter's default value.

<?php 
  function MyFunction($p, $q, $r=0, $s=0){
      return $p+$q+$r+$s; 
  }
  $a = 15; $b = 10; $c = 5; $d = 1;
  echo MyFunction($a,$b)."\n"; 
  echo MyFunction($a,$b,$c)."\n";
  echo MyFunction($a,$b,$c,$d)."\n";
?>

Output

25
30
31

Recursive function

A function which can call itself is known as recursive function. A recursive function generally ends with one or more boundary conditions.

Example:

A function for calculating factorial using recursion method is described below.

<?php 
  function factorial($x){
      if($x==0)
        {return 1;}
      else
        {return $x*factorial($x-1);} 
  }
  echo factorial(3)."\n"; 
  echo factorial(5)."\n";
?>

Output

6
120

Data Type declaration of Parameters of a function

In PHP, there is no need to declare data type of parameters of a function. PHP automatically takes the data type of a variable, depending on its value. This can lead to an error when string is passed through a function instead of numeric data type. Therefore, in PHP 7 and onwards, type declaration is added. This gives an option to specify data type of parameters.

Example:

In the below example, a function called MyFunction is defined which takes integer data type as arguments.

<?php 
  function MyFunction(int $x, int $y){
    $z = $x + $y;
    return $z; 
  }
  
  $a = 15; 
  $b = 10;
  echo MyFunction($a, $b);
?>

Output

25

By adding the strict declaration, it throws "Fatal Error" in case of data type mismatch. This can be done by putting first line of PHP script as declare(strict_types=1);.

<?php 
  declare(strict_types=1);
  function MyFunction(int $x, int $y){
    $z = $x + $y;
    return $z; 
  }
  
  $a = 15.5; 
  $b = 10;
  echo MyFunction($a, $b);
?>

Output

PHP Fatal error:  Uncaught TypeError

Return Type declaration of a function

PHP 7 and onwards supports return data type declaration. It can be achieved by adding a colon ( : ) followed by type declaration.

Example:

In the below example, a function called MyFunction is defined. It is declared to return float data type.

<?php 
  declare(strict_types=1);
  function MyFunction(float $x, int $y) : float{
    $z = $x * $y;
    return $z; 
  }
  
  $a = 15.25; 
  $b = 10;
  echo MyFunction($a, $b);
?>

Output

152.5





Previous Page Next Page
Advertisements