PHP, an odd variable scope?

Only functions create a new local scope. Curly braces by themselves do not. Curly braces are just an auxillary construct for other language structures ( if while or foreach ) And whereever you access any variable in a local scope doesn't matter.

The local scope is an implicit dictionary behind the scenes (see get_defined_vars ). You might get a debug notice by accessing previously undefined variables, but that's about it In your specific example it seems, you are even just operating in the global scope.

Only functions create a new local scope. Curly braces by themselves do not. Curly braces are just an auxillary construct for other language structures (if, while or foreach).

And whereever you access any variable in a local scope doesn't matter. The local scope is an implicit dictionary behind the scenes (see get_defined_vars). You might get a debug notice by accessing previously undefined variables, but that's about it.In your specific example it seems, you are even just operating in the global scope.

That first sentence makes understand it crystal clear! Thanks! – Shyam Apr 21 at 12:20 Cool.

There is also a formal language semantics term for that, but I can't remember. :/ – mario Apr 21 at 12:23.

Foreach does not create any variable scope in PHP so it is natural if variable is available outside foreach for the second question the $a is not equal to the 1 hence $b is not initialized and throw notice when you access outside. If you assign value 1 to $a and test it you will wonder the notices will gone. Here is nothing like scope.

The second example is not a question, more like a perspective as I would expect behavior (my inexperience with the PHP language). As other answers state out, that curly braces do not define the variable scope. Even though, thanks for your answer :) – Shyam Apr 21 at 12:26 @shyam: No problem, As I just tried to explain this – Shakti Singh Apr 21 at 12:30.

See: php.net/manual/en/language.variables.sco... In php curly braces don't necessarily define a new scope for variables. (your first example) In your 2nd example, $b is only set on a specific condition. So it is possible to be 'undefined' if this condition is not met.

Thanks for the link and your answer! – Shyam Apr 21 at 12:26.

Shyam, you are using a scripting language, not C++. It is typical for scripting languages like PHP or JavaScript not to have different scopes for each code block. Instead there is one scope for the whole function.

This is actually quite handy if you consider your first example, but you obviously need to be careful as can be seen in your second one.

Actually your first method should be giving you an error too. You're using a variable that hasn't been declared as an array. I can't understand why you didn't get an error for that.

PHP doesn't have block scope, so whether it's inside IF or FOREACH is irrelevant. If it's available inside the method, you can use it inside the method.

Only functions create a new local scope. Curly braces by themselves do not. Curly braces are just an auxillary construct for other language structures (if, while or foreach).

And whereever you access any variable in a local scope doesn't matter. The local scope is an implicit dictionary behind the scenes (see get_defined_vars). You might get a debug notice by accessing previously undefined variables, but that's about it.

In your specific example it seems, you are even just operating in the global scope.

That's pretty oldschool and not recommended. If you want to use variables whereever you want consider using sessions of passing through variables if you're on an object oriented tour ;).

Alright, thats more or less what I thought. Thanks for the advice. – Dylan Nov 23 '09 at 8:32 You're Welcome :) – Ben Fransen Nov 23 '09 at 8:33.

There is a $GLOBALS variable and a globals keyword. Check out example 1 and 2 on this page in the documentation. But using a global variable is usually a sign of bad structure in your code.

Yah, I was looking at that page earlier. I'll try to come up with a different way to do it. Thanks.

– Dylan Nov 23 '09 at 8:33.

I don't think variables can be globally visible the way you want bnut functions are. You can declare two functions to access you data : on for reading, one for writing. But anyway beware : if you change it from many places you will have hard times understanding what's going on when faced with a bug!

Sometimes a variable available in global scope is not accessible via the 'global' keyword or the $GLOBALS superglobal array. I have not been able to replicate it in original code, but it occurs when a script is run under PHPUnit. PHPUnit provides a variable "$filename" that reflects the name of the file loaded on its command line.

This is available in global scope, but not in object scope. For example, the following phpUnit script (call it GlobalScope. Print "Method scope GLOBALSFILENAME ".

If you run this script via "phpunit GlobalScope. Global scope FILENAME /home/ktyler/GlobalScope. PHPUnit 3.4.5 by Sebastian Bergmann.

Print "Method scope GLOBALSFILENAME ". By doing this, both "global" and $GLOBALS work! I don't know what it is that PHPUnit does (I know it uses Reflection) that causes a globally available variable to be implicitly unavailable via "global" or $GLOBALS.

But there it is.

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