开发者

Variable Not Showing When Function Called Early In Script

开发者 https://www.devze.com 2023-04-13 04:11 出处:网络
Any help is appreciated. I cannot seem to get the value of $sum if I call showSum() before the function...

Any help is appreciated. I cannot seem to get the value of $sum if I call showSum() before the function...

EXAMPLE 1: (this works fine)

[main code here]

$sum = $valueObtainedAfterWhileLoop;

function showSum(){
global $sum;

return 开发者_如何学Go$sum;
}
echo getSum();

EXAMPLE 2: (this does not work - no error is returned, but no value is printed to the screen)

echo getSum();

[main code here]

$sum = $valueObtainedAfterWhileLoop;

function showSum(){
global $sum;

return $sum;
}

I need to use the value of $sum at the top of the page. What can I do?



Absolutely nothing. The variable has not been assigned to yet, so there's no value to use. Try calculating it in the function instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号