PHP Function Procedures
A Function procedure is a series of php statements enclosed together.
A Function takes values, process them, and returns processed result.
A Function procedure can take arguments (variables, or print statements).
Here is a syntax for none-parameterize function:
<?php
function welcome()
{
$greeting="<h4>Welcome to my web site</h4>";
print "$greeting";
}
?>
This function remembers the statement "Welcome to my web site". You can use this function any
number of times in your program and you change it once in here to
maintain. Use welcome() to call this function.
For example;<?php welcome(); ?> will execute the function.
The result will look like:
Welcome to my web site
Here is a syntax for parameterize function:
<?php
Function total($price)
{
$tax=$price*.09;
$total=$price+$tax;
return $total;
}
?>
The price value is provided when calling the function.
eg; echo total(50);
The following example is function procedure that
takes an argument variables (price, cost) and returns calculates profit after tax.
<?php
function profit($sellPrice, $cost)
{
$taxRate=0.09;
$tax=$sellPrice*$taxRate;
$profit=$sellPrice-($cost+$tax);
return $profit;
}
print "Total profit $".profit(100,50);
?>
| Execution result:
Total profit $41
|
|