SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

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.

Discuss this tutorial at forum


Further readings
PHP Function
Declaring variables as global inside the function
Returning variable from a function
Returning an array from a function to main script
Array as input to a function








 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.