Functions 1
Functions 1
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.
The following example has a function with two arguments ($fname and $year):
<?php
functionfamilyName($fname, $year)
{
echo "$fnameRefsnes. Born in $year <br>";
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
Example
<?php
functionsetHeight($minheight = 50)
{
echo "The height is : $minheight<br>";
}
setHeight(350);
setHeight(); "color:green">// will use the default value of 50
setHeight(135);
setHeight(80);
?>