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
}
A simple function is a function without any arguments or parameters.
<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>
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.
<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>
PHP allow us to call the function by value and reference.
In case of call by value, the actual value is not modified even if it is modified inside the function.
<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>
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.
<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>
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.
<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>
<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>
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.
<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>