Re: Variable Scope

From: Date: Thu, 18 Jan 2001 19:34:31 +0000
Subject: Re: Variable Scope
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-35425@lists.php.net to get a copy of this message
I find the VB way (or VBScript anyway) of doing functions backwards. My recollection is that if I call a function blah... x = blahfunction('cow') 'blahfunction' itself has to be defined *in* the function to get the value back. I can't even think of a good example, it's so foreign to me and I haven't done much new vbscript in a while. To get 'x' to equal something, you'd have to have a line in the blahfunction that actually says blahfunction='moo' *That's* weird to me. Anyway, not meant to be bashing, but you brought up VB. What's helped me to think of globals in PHP is that I know variables defined in the main area of the page work on that level. As I go deeper into the code (down a level) into a function, if I need something from a level above, I have to say GLOBAL $varname to make sure I get the varname I want. It's helped me immensely over the last year, but mostly by making me do functions with argument lists and return values, rather than modifying GLOBAL variables. My functions are far more portable between projects than they were a few years ago. "Karl J. Stubsjoen" wrote: > I'm a bit troubled with the fact that variables declared at the top of your > script are not then global to functions within that script. That in order > to see that variable within a function you'd have to declare it as global in > the function (understanding that the variable could have been set at the top > of the script). This seems extremely backwards to me (me being a VB guy). > > Would someone kindly shine the light down upon me with an explanation of > this logic? And why can't I possibly declare the variable as Global at the > top of my script? > > Thats a big "Hmmmm...."

« previous php.general (#35425) next »