Are variables outside functions a kind of global variables?

Yes. They can be accessed from any location, including other scripts. They are slightly better as you have to used the global keyword to access them from within a function, which gives more clarity as to where they are coming from and what they do.

Up vote 1 down vote favorite share g+ share fb share tw.

Php variables global-variables global link|improve this question asked Jun 5 '11 at 2:09Alex2,70411576 98% accept rate.

Yes. They can be accessed from any location, including other scripts. They are slightly better as you have to used the global keyword to access them from within a function, which gives more clarity as to where they are coming from and what they do.

The disadvantages of global variables apply, but this doesn't instantly make them evil as is often perceived in some OO languages. If they produce a good solution that's efficient and easily understandable, then you're fine. There are literally millions of succesful PHP projects that use global variables declared like this.

The biggest mistake you can make is not using them and making your code even more complicated when it would have been perfectly fine to use them in the first place. :D.

Outside of the function is sorta like global scope (when compared to C-like languages), but you have to do one thing to allow access to the var within a function: function meh(){ global $foo; // $foo now exists in this scope }.

In your example $foo gets created as variable in the global scope. (Unless your shown script was included() from within another functions/methods scope. ) PHP doesn't have real global variables.

You have to manually alias it using the global $foo; statement to access them. (Also the "anything global is bad" advise is just that, bad advise. ).

If I understand your question correctly, there really shouldn't be a problem. Unless you declare a variable as a global, it will been limited to the scope in which it is declared, in this case whatever php file the above code is defined in. You could declare another variable $foo in meh() and it would be independent of the $foo defined outside.

All variables defined outside any function are declared in global scope. If you want to access a global variable you have two choices: Use the global keyword Or use the $GLOBALS Read more at php.net/manual/en/language.variables.sco....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions