If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place in the program you can create a task and whenever you want to execute that task You can call that function at a different place if you need to.
By doing this, your time is also saved and computer memory is also saved, as well as your program also becomes short and readable, this is called code re-usability because you can use the same code at different places.
Syntax:-
<?php
function functionName(){
Statement;
}
functionName();
?>
Example:-
<?php
function functionName(){
echo "This is PHP function <br>" ;
}
function newFunction(){
echo "This is New PHP function <br>" ;
}
functionName();
functionName();
echo "Hi this is an example <br>";
newFunction();
newFunction();
?>
Output:-
PHP Functions with Parameters
PHP gives you the option of passing your parameters inside a function so that you can pass more parameters.
<?php
function functionName(parameter1,parameter2){
Statement;
}
functionName(argument1,argument2);
?>
Example:-
<?php
function sum($a, $b){
$sum = $a + $b;
echo "This in parameter function $sum" ;
}
sum(10,20);
?>
Output:-