| PHP Functions |
| |
| Returning values from function |
| |
| A single value may be returned from a PHP function to the script from which it was called. The returned value can be any variable type of your choice. |
| |
| Besides being able to pass functions information, you can also have them return a value. However, a function can only return one thing, although that thing can be any integer, float, array, string, etc. that you choose! |
| |
| How does it return a value though? Well, when the function is used and finishes executing, it sort of changes from being a function name into being a value. To capture this value you can set a variable equal to the function. |
| |
| Something like: |
| |
. $myVar = somefunction();
Let's demonstrate this returning of a value by using a simple function that returns the sum of two integers. |
| |
| The return keyword is used to return the value: |
| |
<HTML>
<HEAD>
<TITLE>PHP Function: Returning Values
</TITLE></HEAD>
<BODY>
<?php
function sum ($val1, $val2)
{
$result=$val1+$val2;
return $result;
}
?>
<hr>
<CENTER><H3>PHP Function: Returning Values</H3></CENTER>
<hr>
<p>Sum of 21 and 79 =<?php echo " ".sum(21,79);?></p>
</BODY>
</HTML>
|
| |
| The above example returns the value of 100 to the calling script. |
| |
| |
|
| |
| |