Returning value from a Functions to main script in PHP |
Now let us modify the function we discussed and see how we can return value from a php function to our main script. Here we will send three numeric values to the function and get back the sum of the three values in our main script. Here the function will receive three arguments or variables and process it. We will use return command to return the end result to the main script from the function.
<?
function my_function($var1,$var2,$var3){
// Place to enter code here.
$var=$var1+$var2+$var3;
return $var;
}
$my_val=my_function(5,6,8);
echo "The return value= $my_val";
// The above line will print the sum of the three variables.
?>
We have seen here how the value is returned to the main calling script by using return command. To access the variables declared in the main script inside the function, we have to declare them as global inside the function.
|