Why does this global not work??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Why does this global not work??

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Is $Total defined outside this function? Also "Global" and "Return" should be lowercase.
(#10850)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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...
Post Reply