PHP FUNCTIONS
A function will be executed by a call to the function.
You may call a function from anywhere within a page.
CREATE A PHP FUNCTIONS
A function will be executed by a call to the function.
Syntax
function functionName()
{
code to be executed;
}
Example
A simple function that writes my name when it is called:
function writeName()
{
echo "kem john Refsnes";
}
echo "My name is ";
writeName();
OUTPUT:
My name is Kem john
PHP FUNCTIONS - ADDING PARAMETERS
To add more functionality to a function, we can add parameters. A parameter is just like a variable.Parameters are specified after the function name, inside the parentheses.
Example:
The following example will write different first names, but equal last name:
function writeName($fname)
{
echo $fname . " Refsnes";
}
echo "My name is ";
writeName("John");
echo "My sister's name is ";
writeName("Alba");
echo "My brother's name is ";
writeName("Kem");
OUTPUT:
My name is John Refsnes.
My sister's name is Alba Refsnes.
My brother's name is Kem Refsnes.
PHP FUNCTIONS - RETURN VALUES
To let a function return a value, use the return statement.
Example
function add($a,$b)
{
$totalvalue=$a+$b;
return $totalvalue;
}
echo "4 + 22 = " . add(4,22);
Output:
4 + 22 = 26