Page 92 - Open Soource Technologies 304.indd
P. 92
Web Technologies-I
Notes to do that, we need to return the value of $hourly_pay from the function and that’s just done
with simply the word return, that’s the function that we are calling. We are telling the function,
return this value!
It is also going to exit out of the function at that point when we say return. It is a lot like break was
when we were working with the loop. Let’s update our overtime () function so that it can return
a value out of the function. The only thing that you should do is to replace the word “echo” with
“return” and some modification in how we call the function.
<html>
<head>
<title>Writing Functions In PHP Return Values</title>
</head>
<body>
<?php
/* declaring our third function */
function overtime($salary, $month, $day, $rate){
$hourly_pay = ($salary / $month / $day) * $rate;
return $hourly_pay;
}
$returned_value = overtime(2000,24,8, 1.5);
echo $returned_value;
?>
</body>
</html>
So we called the function overtime(), we are going to return the value $hourly_pay to $returned_
value and then echo back the new value.
Now, it’s a good idea whenever we are working with functions, especially functions that do not
just do display like we did with say_hello(), that you always return a value out of them. Just get
in a habit of making sure every function has a return. May be all it returns is true or false based
on whether it works successfully, but we want to have something returned out of the end of that
function just to let you know.
So for our say_hello() function, we should write something like this:
<?php
function say_hello(){
echo “Hello World!”;
return true;
}
say_hello();
?>
86 LOVELY PROFESSIONAL UNIVERSITY