PHP Tutorial: PHP – Functions

PHP – Functions

PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are thousands of built-in functions in PHP.
In PHP, we can define Conditional function, Function within Function and Recursive function also.

PHP gives you option to create your own functions as well.

There are two parts which should be clear to you −

  • Creating a PHP Function
  • Calling a PHP Function

Advantage of PHP Functions

Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages.
Less Code: It saves a lot of code because you don't need to write the logic many times. By the use of function, you can write the logic only once and reuse it.
Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.

PHP User-defined Functions

We can declare and call user-defined functions easily. Let's see the syntax to declare user-defined functions.

Syntax:

<?php
function functionname(){

//code to be executed

}
?>

Note: Function name must be start with letter and underscore only like other labels in PHP. It can't be start with numbers or special symbols.

Example:-

<?php  
function sayHello(){  
   echo "Hello PHP Function";  
}  
sayHello();//calling function  
?> 

Output:

Hello PHP Function

PHP Function Arguments:
We can pass the information in PHP function through arguments which is separated by comma.
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list.
Let's see the example to pass single argument in PHP function.

<?php  
function sayHello($name){  
  echo "Hello $name<br/>";  
}  

sayHello("neha");  
sayHello("rakhi");  
sayHello("Harsha");  
?> 

Output:
Hello neha
Hello rakhi
Hello Harsha

 

Let's see the example to pass two argument in PHP function.

<?php  
function sayHello($name,$age){  
echo "Hello $name, you are $age years old<br/>";  
}  
sayHello("Ram",24);  
sayHello("shyam",23);  
sayHello("Sita",30);  
?>  

Output:
Hello Ram, you are 24 years old
Hello shyam, you are 23 years old
Hello Sita, you are 30 years old

 

PHP Call By Reference:
Value passed to the function doesn't modify the actual value by default (call by value). But we can do so by passing value as a reference.
By default, value passed to the function is call by value. To pass value as a reference, you need to use ampersand (&) symbol before the argument name.
Let's see a simple example of call by reference in PHP.

<?php  
function adder(&$str2)  
{  
    $str2 .= 'Call By Reference';  
}  
$str = 'Hello ';  
adder($str);  
echo $str;  
?>


Output:
Hello Call By Reference

 

PHP Function: Default Argument Value
We can specify a default argument value in function. While calling PHP function if you don't specify any argument, it will take the default argument. Let's see a simple example of using default argument value in PHP function.

<?php  
function sayHello($name="Sonoo"){  
echo "Hello $name<br/>";  
}  
sayHello("Rajesh");  
sayHello();//passing no value  
sayHello("John");  
?>  

Output:
Hello Rajesh
Hello Sonoo
Hello John

 

PHP Function: Returning Value

Let's see an example of PHP function that returns value.

<?php  
function cube($n){  
return $n*$n*$n;  
}  
echo "Cube of 3 is: ".cube(3);  
?>  

Output:
Cube of 3 is: 27

PHP Call By Value:
Call by value means passing the value directly to a function. The called function uses the value in a local variable; any changes to it do not affect the source variable. Let's understand the concept of call by value by the help of examples.
Example :-
In this example, variable $str is passed to the adder function where it is concatenated with 'Call By Value' string. But, printing $str variable results 'Hello' only. It is because changes are done in the local variable $str2 only. It doesn't reflect to $str variable.
 

<?php  
function adder($str2)  
{  
    $str2 .= 'Call By Value';  
}  
$str = 'Hello ';  
adder($str);  
echo $str;  
?> 

Output:
Hello

Example 2:-
Let's understand PHP call by value concept through another example.

<?php  
function increment($i)  
{  
    $i++;  
}  
$i = 10;  
increment($i);  
echo $i;  
?>

Output:
10

 

PHP Call By Reference:
Call by reference means passing the address of a variable where the actual value is stored. The called function uses the value stored in the passed address; any changes to it do affect the source variable.

Example 1
In this example, variable $str is passed to the adder function where it is concatenated with 'Call By Reference' string. Here, printing $str variable results 'This is Call By Reference'. It is because changes are done in the actual variable $str.

<?php  
function adder(&$str2)  
{  
    $str2 .= 'Call By Reference';  
}  
$str = 'This is ';  
adder($str);  
echo $str;  
?>  

Output:
This is Call By Reference

 

PHP Recursive Function:
A recursive function is a function that calls itself
Example 1: Printing number

<?php    
function display($number) {    
    if($number<=5){    
     echo "$number <br/>";    
     display($number+1);    
    }  
}    
    
display(1);    
?>

Output:
 1
 2
 3
 4
 5

Example 2 : Factorial Number

<?php    
function factorial($n)    
{    
    if ($n < 0)    
        return -1; /*Wrong value*/    
    if ($n == 0)    
        return 1; /*Terminating condition*/    
    return ($n * factorial ($n -1));    
}    
    
echo factorial(5);    
?>  

Output: 120

Next Topic