My understanding of how PHP handles variable scope is that you should declare as global any variable that will be passed into functions, and within the function also declare it as global.  If that's not correct I can change it, but I have seen things break weirdly with the variables being "empty" unexpectedly if not defined this way.