Page 1 of 1
Why does this global not work??
Posted: Wed Feb 08, 2006 9:25 am
by Benjamin
I have a function like so;
Code: Select all
function do_blah() {
Global $Connect, $Total;
while (blah blah) {
$Total++;
echo "debug\n";
}
Return $IsBeingUsedBySomethingElse;
}
It loops through x number of times and prints debug a bunch of times, but $Total is always null when I try to get the count out of it.
Posted: Wed Feb 08, 2006 9:31 am
by feyd
why not make it apart of the return data? I'm curious how you come to the conclusion that $Total is null. In some instances, if a variable is zero, php will not print anything for it. Try using
var_export() to make sure.
Posted: Wed Feb 08, 2006 9:44 am
by Benjamin
It was a bug someplace else, what was happening is that it was trying to return the value of $Total before the function was being called. I couldn't return it with the function because I don't want to risk breaking the code I wrote which outputs it into a table.
Posted: Wed Feb 08, 2006 11:42 am
by Christopher
Is $Total defined outside this function? Also "Global" and "Return" should be lowercase.
Posted: Wed Feb 08, 2006 3:11 pm
by raghavan20
passing global variables as parameters should be the optimal way to do it...why do you use global variables...please reconsider...or you might have to change all your code later on...