The function performs a specific task or block of code the main advantages of using functions is Reducing duplication of code, Improving clarity of the code, Reuse of code, Information hiding and more advantages
<?php
function (arguments)
{
//Code Here
}
<function name> //call function
?>
<?php
function demo()
{
echo "Hello This is Demo Function";
}
demo(); //call function
?>
php also support user define function the following list of user define function available in php that explain in details.
1. No Arguments No Return Value
2. No Arguments With Return Value
3. With Arguments No Return Value
4. With Arguments With Return Value
In this function can not pass arguments and no return value it simple function that function call and display function data the following example of no arg and no return value function.
<?php
function add()
{
$a=10;
$b=20;
echo $a+$b;
}
add();
?>
In this function not pass arguments but return value the following example of no arguments and with return value.
<?php
function sub()
{
$a=10;
$b=20;
return $a-$b;
}
echo sub();
?>
in this function can pass arguments but not return a value following example of with arguments and no return value.
<?php
function mul($a,$b)
{
echo $a*$b;
}
$a=10;
$b=20;
mul($a,$b);
?>
In this function can pass arguments and return a value the following example of with arguments and return value.
<?php
function mul($a,$b)
{
return ($a/$b);
}
$a=10;
$b=20;
echo div($a,$b);
?>