SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Functions (I)

PHP Example #50

Functions

Functions are pieces of source code that are identified by a name; they allow the reutilization of the source code encompassed in them, so habitual functions can be performed without having to copy and paste the same pieces of code again and again... We have already used in the previous examples many of the functions that are provided by the PHP interpreter. Now we will learn to write our own handy functions. In this example, I wrote the code of one of the earlier examples inside the function (the code assigns data to an array and prints a table with that data), so each time I call the function, the table will be printed.

Functions can be defined before or after calling them, because the PHP interpreter will read all the definitions before executing any command.

<?php
function print_table() {
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
print '<table summary="Dishes">';
foreach ($meal as $key => $value) {
print "<tr><td>$key</td><td>$value</td></tr>\n";
}
print '</table>';
}
print_table();
?>
breakfastWalnut Bun
lunchCashew Nuts and White Mushrooms
snackDried Mulberries
dinnerEggplant with Chili Sauce

PHP Example #51

Functions with parameters

In this example we will see an improved version of the function; we will assign parameters to it, so we can change the values displayed in the table and also its color. With this we have a more flexible and therefore useful function, but each time we call it, we must supply all the arguments, except if we have supplied arguments by default, as in the example, where $color is an argument by default. This allows us to call the function without passing any particular color, and the default one will be used in that case. When defining a function, optional arguments must be placed always after the mandatory arguments.

Any change performed in a variable passed as an argument inside a function, will not affect the variable outside the function, even if both variables have the same name, for there is no connection between them. Variables that have their domain inside a function are called local variables, while variables that are outside functions are called global variables. So we could have a variable called $dinner inside a function or multiple functions and another variable with the same name outside the functions.

<?php
function print_color_table($breakfast, $lunch, $snack, $dinner, $color = '80FF80') {
$meal = array('breakfast' =>> "$breakfast",
'lunch' => "$lunch",
'snack' => "$snack",
'dinner' => "$dinner");
print '<table summary="Dishes">';
foreach ($meal as $key => $value) {
print '<tr bgcolor="#' . $color . '">';
print "<td>$key</td><td>$value</td></tr>\n";
}
print '</table>';
}
print_color_table('Walnut Bun and Orange Juice',
'Cashew Nuts, White Mushrooms and Red Wine',
'Dried Mulberries and Coffee',
'Eggplant with Chili Sauce and Beer');
print nl2br("\n\n");
print_color_table('Walnut Bun and Orange Juice',
'Cashew Nuts, White Mushrooms and Red Wine',
'Dried Mulberries and Coffee',
'Eggplant with Chili Sauce and Beer',
'FF8080');
?>
breakfastWalnut Bun and Orange Juice
lunchCashew Nuts, White Mushrooms and Red Wine
snackDried Mulberries and Coffee
dinnerEggplant with Chili Sauce and Beer


breakfastWalnut Bun and Orange Juice
lunchCashew Nuts, White Mushrooms and Red Wine
snackDried Mulberries and Coffee
dinnerEggplant with Chili Sauce and Beer

PHP Example #52

Functions that return values

We can use the return statement to indicate to a function that it should return a value; this value can be assigned to any variable and therefore be used for any purpose. The function will end its execution as soon as the return command is found.

<?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;
}
// Find out the total cost of a $15.22 meal with 8.25% taxes and a 15% tip
$total_cost = restaurant_check(15.22, 8.25, 15);
printf('The dish costs $%.2f', $total_cost);
?>
The dish costs $18.76

PHP Example #53

Functions that return an entire array

A function can't return multiple values in the form of individual variables, but this is not problem, since it can return an array with all its content.

<?php
function restaurant_check2($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100);
$tip_amount = $meal * ($tip / 100);
$total_notip = $meal + $tax_amount;
$total_tip = $meal + $tax_amount + $tip_amount;
return array($total_notip, $total_tip);
}
$total_cost = restaurant_check2(15.22, 8.25, 15);
printf('The dish costs $%.2f without a tip and $%.2f including the tip', $total_cost[0], $total_cost[1]);
?>
The dish costs $16.48 without a tip and $18.76 including the tip.