SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Functions (II)

PHP Example #54

Functions that have multiple return statements

Despite a function will terminate its execution when a return statement is found, functions can have multiple return commands to choose from, depending in decision structures. In the example , we use a function to calculate the total price of a meal; the returned value is passed as an argument to a second function that will return the payment method.

<?php
function restaurant_check($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100);
$tip_amount = $meal * ($tip / 100);
$total_amount = $meal + $tax_amount + $tip_amount;
return $total_amount;
}
function payment_method($cash_on_hand, $total_amount) {
if ($total_amount > $cash_on_hand) {
return 'Payment with credit card is available.';
} else {
return 'Payment with cash is mandatory.';
}
}
$total_cost = restaurant_check(15.22, 8.25, 15);
print payment_method(20, $total_cost);
?>
Payment with cash is mandatory.

PHP Example #55

Using return values as regular variables

Calls to functions can be used inside statements as any other regular variable. In this example we can see how the call to the restaurant_check2() function is used directly as if it were a regular variable, both inside an if statement and as an argument for another function.

<?php
if (restaurant_check(15.22, 8.25, 15) < 20) {
print 'Payment with cash is mandatory.';
} else {
print 'Payment with credit card is available.';
}
print nl2br("\n\n");
print payment_method(20, restaurant_check(15.22, 8.25, 15));
?>
Payment with cash is mandatory.

Payment with cash is mandatory.

PHP Example #56

Return values as true or false

Return values can be something so simple as a mere binary concept: true or false. In this example we call the function restaurant_check2() from inside the function can_pay_cash() and use the result of the testing to return a binary value; then the function can_pay_cash() is called inside an if statement to print the corresponding informative sentence. As you can see, during the execution of a program, the call of a function that returns a value is replaced by the value that it returns, and is equivalent to a regular variable.

<?php
function can_pay_cash($cash_on_hand) {
if (restaurant_check(15.22, 8.25, 15) < $cash_on_hand) {
return true;
} else {
return false;
}
}
if (can_pay_cash(20)) {
print 'Payment with cash is mandatory.';
} else {
print 'Payment with credit card is available.';
}
?>
Payment with cash is mandatory.

PHP Example #57

Accessing global variables from inside functions

In a previous example (#51) I have explained about the scope of variables. Local variables can't be accessed from outside the function where they belong, but global variables can be accessed from inside any function if they are called by using the special array $GLOBALS[], that stores (or points to) every global variable that is in use. This simple example illustrates perfectly how this works.

$GLOBALS['dinner'] is exactly the same thing than $dinner, and this equivalence is true for every global variable; any change applied to any of them will have the same effect in its "alias". $GLOBALS[] is a type of variable known as auto-global; there are several of these variables which perform different functions, such as $_POST[], that we saw already in previous examples.

<?php
$dinner = 'Curry Cuttlefish';
function hungry_dinner() {
$GLOBALS['dinner'] .= ' and Deep-Fried Taro';
}
print "Regular dinner is $dinner.\n";
hungry_dinner();
print "Hungry dinner is $dinner";
?>
Regular dinner is Curry Cuttlefish. Hungry dinner is Curry Cuttlefish and Deep-Fried Taro

PHP Example #58

Accessing global variables from inside functions

Another way of accessing a global variable from inside a function is the keyword 'global', which indicates to the PHP interpreter that the subsequent use of the named variable inside the function refers to the global variable which has that name and not to a local one with such name. This is referred as 'bringing a variable to the local scope'. You can also assign multiple variables to the 'global' statement, simply by separating them with commas (like the arguments in functions).

Generally, the utilization of the array $GLOBALS[] is recommended upon the usage of the 'global' statement, because with 'global' it is probable to get confused as the program increases in complexity.

<?php
$dinner = 'Curry Cuttlefish';
function hungry_dinner2() {
global $dinner;
$dinner .= ' and Deep-Fried Taro';
}
print "Regular dinner is $dinner.\n";
hungry_dinner2();
print "Hungry dinner is $dinner";
?>
Regular dinner is Curry Cuttlefish. Hungry dinner is Curry Cuttlefish and Deep-Fried Taro