PHP Menu


Functions in PHP


A function is a piece of code or self contained block of code that is used to perform a particular task.

The main advantage of functions is that code reusability (Write once Invoke multiple).

PHP supports both built-in and user-defined functions. And PHP supports thousands of built-in functions and allows us to define own functions, by using "function" keyword.


Syntax:

function fun_name ()
{
	//code
}

 Note : Function name must be starts with a letter or underscore only.

Simple Function

A simple function is a function without any arguments or parameters.

Example: "FunSimple.php"


<html>

<head>
<title>Function Example</title>
</head>
<body>
<h1>Simple Function Example</h1>
<?php
function simple() //Function Definition 
{  
    echo 
"Welcome to php";
}  
simple();//Function calling
?>
</body>
</html>

Output :

image

Parameterized Functions

A Parameterized functions are functions with parameters.i.e. These functions allow us to pass one or more parameters inside the function.

We can pass the information in function through arguments (or) parameters which are separated by comma (,).

These passed arguments (or) parameters acts as variables inside the function.

Example: "FunParam.php"


<html>

<head>
<title>Function Example</title>
</head>
<body>
<h1>Parameterized Function Example</h1>
<?php
function add($a,$b//function defn
{
    
$sum $a $b;
    echo 
"Sum is :$sum <br/>";
}
add(23,34);//calling function

function sub($a,$b//function defn
{
    
$sub $a $b;
    echo 
"Sub is :$sub";
}
sub(63,34);//calling function
?>
</body>
</html>

Output :

image

Function Call by Value & Reference

PHP allow us to call the function by value and reference.

☞ Call by Value:

In case of call by value, the actual value is not modified even if it is modified inside the function.

Example: "FunCallByValue.php"


<html>

<head>
<title>Call by Value</title>
</head>
<body>
<h1>Call by Value Demo</h1>
<?php
//Example1
echo "Example1 : ";
function 
adder($str)  
{  
    
$str .= 'Call By Value';  
}  
$str 'Hello ';  
adder($str);  
echo 
$str
echo 
"<br/>";

//Example2
echo "Example2 : ";
function 
increment($i)  
{  
    
$i++;  
}  
$i 10;  
increment($i);  
echo 
$i
?>
</body>
</html>

Output :

image

☞ Call by Reference:

In case of call by reference, the actual value is modified if it is modified inside the function.

In this case, we need to use & (ampersand) symbol with formal arguments or parameters. And & (ampersand) symbol represents reference of a variable.

Example: "FunCallByReference.php"


<html>

<head>
<title>Call by Reference</title>
</head>
<body>
<h1>Call by Reference Demo</h1>
<?php
//Example1
echo "Example1 : ";
function 
adder(&$str2)  
{  
    
$str2 .= 'Call By Reference';  
}  
$str 'Hello ';  
adder($str);  
echo 
$str
echo 
"<br/>";

//Example2
echo "Example2 : ";
function 
increment(&$i)  
{  
    
$i++;  
}  
$i 10;  
increment($i);  
echo 
$i;  
?>
</body>
</html>

Output :

image

Default argument function

PHP allow us to define default argument values in the function. In this case if we don’t pass any value to the function, it will use default argument value.

Example1: "FunDefaultParam.php"


<html>

<head>
<title>Default Argument Function</title>
</head>
<body>
<h1>Default Argument Function Demo</h1>
<?php
function msg($name="Study"//Function Definition 
{  
    echo 
"Welcome to $name <br/>";
}  
msg();    //Function calling without parameter
msg("Glance");    //Function calling with parameter
?>
</body>
</html>

Output :

image

Example2: "FunDefaultParamd.php"


<html>

<head>
<title>Default Argument Function</title>
</head>
<body>
<h1>Default Argument Function Demo</h1>
<?php
function add($a=10$b=10//Function Definition 

    
$sum $a $b;
    echo 
"Addition is : $sum <br/>";
}  
add();    //Function calling with no parameter
add(20);    //Function calling with 1 parameter
add(20,40);    //Function calling with 2 parameter
?>
</body>
</html>

Output :

image

Recursive function

PHP allow us to use recursive function. In this case, we call the current function within the same function. It is also known as recursion.

Example: "FunRecursive.php"


<html>

<head>
<title>Recursive Function Example</title>
</head>
<body>
<h1>Recursive Function Demo</h1>
<?php
function factorial($n)
{
    if(
$n 0)
        return -
1;
    if(
$n == 0)
        return 
1;
    return(
$n factorial($n 1));
}
echo 
factorial(5);
?>
</body>
</html>

Output :

image
Next Topic :Form Handing in PHP