質問

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

他のヒント

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;

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top