Creating a simple function
Let's create two functions that will print the string "PHP Functions" five times, but the first one will not contain parameters, and the second one will. A function parameter is nothing more than a piece of data that the function requires to execute. In above example also included code to call the function.
<?php
function firstFunction()
{
for($i = 0; $i != 5; $i++)
echo "<P>PHP Functions!</P>";
}
//let's add parameters to that function
function secondFunction($num, $msg)
{
for($i = 0; $i != $num; $i++)
echo "<P>". $msg ."</P>";
}
echo "This is before the functions is called<br>";
echo "The first function output is:"
firstFuction(5,"This is a function with parameters");
echo "The second function output is:";
secondFuction(5,"This is a function with parameters");
echo "This is after the function has been called<br>";
?>
Next example creates a function that will calculate arithmetic mean and return a result with the "return value" statement:
<?php
function aritmetic_mean($a, $b)
{
$result = ( $a + $b ) / 2;
return $result;
}
//print the the results of calculation
echo aritmetic_mean(4,6),"<br>";
echo aritmetica_mean(3242,524543),"<br>";
?>