Introduction To PHP Funct.8776715.powerpoint
Introduction To PHP Funct.8776715.powerpoint
function helloWorld(){
echo "HelloWorld";
//No out put is shown
}
Functions: Passing Parameters
Passing parameters by value
– function salestax($price,$tax) {
$total = $price + ($price * $tax
echo "Total cost: $total";
}
salestax(15.00,.075);
$pricetag = 15.00;
$salestax = .075;
salestax($pricetag, $salestax);
Functions: Passing Parameters
Passing parameters by reference
$cost = 20.00;
$tax = 0.05;
function calculate_cost(&$cost, $tax)
{
// Modify the $cost variable
$cost = $cost + ($cost * $tax);
// Perform some random change to the $tax
variable.
$tax += 4;
}
calculate_cost($cost,$tax);
echo "Tax is: ". $tax*100."<br />";
echo "Cost is: $". $cost."<br />";
Functions: Default Argument Values
Default values are automatically assigned to the
argument if no other value is provided
$price = 15.47;
salestax($price);
Functions: Optional Arguments
Certain arguments can be designated as
optional by placing them at the end of the list
and assigning them a default value of nothing .
salestax(42.00);
function calculate($price,$price2="",$price3="")
{
echo $price + $price2 + $price3;
Functions: Returning Values
You can pass data back to the caller by way of the
return keyword.
function salestax($price,$tax=.0575) {
function retrieve_user_profile() {
$user[] = "Jason";
$user[] = "[email protected]";
return $user; }
list($name,$email) = retrieve_user_profile();
echo "Name: $name, email: $email ";
Functions: Nesting Functions
defining and invoking functions within functions
function salestax($price,$tax)
function convert_pound($dollars,
$conversion=1.6) {
return $dollars *
$conversion;
}
$total = $price + ($price * $tax);
echo "Total cost in dollars: $total. Cost in
British pounds: "
. convert_pound($total); }
salestax(15.00,.075);
echo convert_pound(15);
Functions: Recursive Functions
functions that call themselves
function nfact($n) {
if ($n == 0) {
return 1;
}else {
return $n * nfact($n - 1);
}
}
//call to function
nfact($num) ;