Get latest news and Web Designing tutoria

Saturday, 30 May 2015

Getting values out of PHP functions

When you're creating your own functions, you may notice that they can be broken down in to two categories: functions that you can leave, and just let them do their jobs; and functions where you need to get an answer back. As an example, here's the two different categories in action:
print ("Get on with it!");
$string_length = strlen($string_length);

The print function is an example of a function that you can leave, and just let it do its job. You just tell it what to print and it gets on with it for you. But a function like strlen( ) is not. You need something back from it – the length of the string.
Suppose you had a function that worked out a 10 percent discount. But you only want to apply the discount if the customer spent over 100 pounds. You could create a function that is handed the amount spent. Then check to see if it's over a 100 pounds. If it is, the function calculates the discount; if not, don't apply the discount. But in both cases, you want the function to return the answer to your question – What do I charge this customer? Here's the script:
<?PHP
$total_spent = 120;
$order_total = calculate_total($total_spent);
print $order_total;
function calculate_total($total_spent) {
$discount = 0.1;
if ($total_spent > 100) {
$discount_total = $total_spent - ($total_spent * $discount);
$total_charged = $discount_total;
}
else {
$total_charged = $total_spent;
}
return $total_charged;
}
?>
The lines to concentrate on are the ones for the $total_spent variable. The code first sets up a total amount spent, which in practice may come from a form on a text box, or a hidden field:
$total_spent = 120;
The next line is our function call:
$order_total = calculate_total($total_spent);
The function call is now on the right of the equals sign ( = ). To the left of the equals sign is just a normal variable - $order_total . If you're setting up your function like this then you are asking PHP to return a value from your functions, and put the answer into a variable on the left of the equals sign. PHP will go off and calculate your function. When it's found an answer, it will try to return a value. The answer will be stored in the name of your function, calculate_total( ) for us. But look at the function itself, and the line at the end:
function calculate_total($total_spent) {
$discount = 0.1;
if ($total_spent > 100) {
$discount_total = $total_spent - ($total_spent * $discount);
$total_charged = $discount_total;
}
else {
$total_charged = $total_spent;
}
return $total_charged;
}
The last line is:
return $total_charged;
The return word tells PHP to return a value. The value it returns is whatever you have stored in the variable that comes after the word return. Here, were telling PHP to set the answer to the function called calculate_total( ) to whatever is stored in the variable we've called $total_charged. It's this that will get stored in our variable called $order_total.
If you're finding this a bit tricky, remember what a function is: a separate piece of code that does some work for you. It can either return a value, or not return a value. It depends entirely on your needs. But don't worry about the majority of the code above – just concentrate on the coloured parts.
In the script above, you'd want to get something back from the function, rather than letting it just print something out. If you ran the previous script, you'll notice that the function prints out the same thing twice. To stop that happening, we can get a return value, and put it in a variable. We can then check what is coming back from the function, to check what's in it.

0 comments:

Post a Comment