-
A function is a way of wrapping up a some part of code(Chunk) and giving that small name(Chunk), so that you can use chunk that later in just one line of code.
-
It is more useful when using code morethan one place.
Syntax :
function function-name ($argument-1, $argument-2, ..)
{
statement-1;
statement-2;
...
}
PHP Functions - Adding parameters
Example :
<?php
function writeName($fname)
{
echo $fname . " Hi.
";
}
echo "My name is ";
writeName("Siddle");
echo "My sister's name is ";
writeName("Lita");
echo "My brother's name is ";
writeName("Peter");
?>
Output :
My name is Siddle Hi.
My sister's name is Lita Hi.
My brother's name is Peter Hi.
PHP Functions - Return values
Example :
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "2 + 18 = " . add(2,18);
?>
Output :
2 + 18 = 20