currently I have the following code
inside my ‘function.php’ is
function calcTime($database_name,$currentTime){
global $startTime;
global $endTime;
...calcutions
return $startTime;
return $endTime;
}//end calcTime()
inside my main ‘index.php’ I have
include('/function.php');
$databaseName = foo;
$currentTime = 12.30;
function begin($database_name,$currentTime){
...some calculations
calcTime($database_name,$currentTime); //calling the function from other file
echo $startTime;
echo $endTime;
}// end begin()
The problem I am having is that the variables that are declared inside the inner function do not pass through to the outer function. I have declared the variables globals and returned them. Not sure whats going on.
something interesting though, if I echo calcTime($database_name,$currentTime); the $startTime is returned but not $endTime.
Please help. I have functions that are used throughout other functions that I would like to use in this fashion. Thank you!!
The
globalkeyword in PHP is used to access global variables which were declared outside of the function. It’s syntactic sugar for writing$var =& $GLOBALS['var'].There are at least two options how you could return your two variables from your function: call-by-ref or returning an array:
Or, passing the arguments as references: