1

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!!

2
  • I am not an expert in PHP global but at least I know that you do not have to return what’s global, or do not have to set global, what you return. I think (that’s where I am uncertain) that global is used when you have a variable that already exists in the outer code before you use it inside a function. But in your code $startTime is not used before the function call. Just use list($startTime, $endTime) = calcTime($database_name,$currentTime); and remove the two global commands. And of course change the two return to return array($startTime, $endTime); Commented Jul 14, 2012 at 19:12
  • You can only ever return once from a function. You may however return an array of values or an object. Commented Jul 14, 2012 at 19:14

3 Answers 3

2

The global keyword 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:

function calcTime($database_name,$currentTime){
    return array('start' => $startTime, 'end' =>  $endTime);
}

$times = calcTime(…, …);
echo $times['start'], ' and ', $times['end'];
// or:
list($start, $end) = calcTime(…, …);
echo $start, ' and ', $end;

Or, passing the arguments as references:

function calcTime($database_name,$currentTime, &$startTime, &$endTime){
    $startTime = …;
    $endTime = …;
}

$startTime = 0;
$endTime = 0;
calcTime(…, …, $startTime, $endTime);
echo $startTime, ' and ', $endTime;
Sign up to request clarification or add additional context in comments.

Comments

2

The first issue is that global in PHP is a bit counter-intuitive. I know it confused me. It doesn't make a variable inside the function accessible outside; rather, it allows you to use variables in the function which were declared outside, eg:

$foo = 'hello';
function bar() {
    global $foo;
    echo $foo;
}

What you want to do is return both the variables. However, you can only return once. As soon as PHP reaches a return statement, it ends the function, so the second one never runs.

What I'd advise doing is returning an array containing both values, eg:

return array('startTime' => $startTime, 'endTime' => $endTime);

Then, you can use extract to make them variables again:

extract( calcTime() );
echo $startTime;
echo $endTime;

2 Comments

Thank you for explaining the issue with globals. I had no idea. Whats the difference between declaring a global inside the function or passing the variable through the function? for instance, in your example above, why not just function bar($foo) ? Would that be the same or is there a benefit to doing it that way?
Fewer globals is better. If you can pass a variable as parameter, do it.
2

Simply put: the calling scope isn't global scope either... So for it to work, the variables would have to be declared global there as well. Do note that (over)use of globals is considered bad practice, and a hell to debug for you fellow coders (or even you after some time has passed). Favor parameters & returns.

** remember every function is isolated on php, when you set a global or variable on one function that will be visible only on function scope

Of course, you can return only one value, but that might as well be a more complex one, like:

return array('startTime' => $startTime,'endTime' => $endTime);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.