Function

By ukmodak | March 31st 2024 10:36:23 AM | viewed 500 times

Function

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.

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
Syntax
    function functionname(){  
    //code to be executed  
    }  
function sayHello(){  
echo "Hello PHP Function";  
}  
sayHello();//calling function  

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.

function sayHello($name){  
echo "Hello $name
"; } sayHello("Sonoo"); sayHello("Vimal"); sayHello("John"); op: Hello Sonoo Hello Vimal Hello John function sayHello($name,$age){ echo "Hello $name, you are $age years old
"; } sayHello("Sonoo",27); sayHello("Vimal",29); sayHello("John",23); op: Hello Sonoo, you are 27 years old Hello Vimal, you are 29 years old Hello John, you are 23 years old

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.

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

op:
Hello Call By Reference 

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.

function sayHello($name="Sonoo"){  
echo "Hello $name
"; } sayHello("Rajesh"); sayHello();//passing no value sayHello("John"); op: Hello Rajesh Hello Sonoo Hello John function cube($n){ return $n*$n*$n; } echo "Cube of 3 is: ".cube(3); op: Cube of 3 is: 27

Variable Length Argument Function

PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do so, you need to use 3 ellipses (dots) before the argument name.

The 3 dot concept is implemented for variable length argument since PHP 5.6.

function add(...$numbers) {  
    $sum = 0;  
    foreach ($numbers as $n) {  
        $sum += $n;  
    }  
    return $sum;  
}  
  
echo add(1, 2, 3, 4);  

op:
10 

Recursive Function

PHP also supports recursive function call like C/C++. In such case, we call current function within function. It is also known as recursion.

It is recommended to avoid recursive function call over 200 recursion level because it may smash the stack and may cause the termination of script.

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

op:
1
2
3
4
5

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);   

op:
120
bONEandALL
Visitor

Total : 27273

Today :9

Today Visit Country :

  • United States
  • Sweden
  • Singapore
  • Russia