Understanding variable scopes in php?

In my understanding, the scope of $a and $b should end within the curly braces { }! Only functions and methods have their own, local scope. Other control structures (loops, conditions...) do not Variable scope in the PHP manual.

In my understanding, the scope of $a and $b should end within the curly braces { }! Only functions and methods have their own, local scope. Other control structures (loops, conditions...) do not.

Variable scope in the PHP manual.

Tzmatt7447 To see why this is the desired behaviour, think of code like if (1Php is fine with stuff like $ar = 7, which declares an array, creates it and adds its first element all in one go. – Michael Clerx Aug 30 '10 at 21:12.

The first $a and $b would actually throw a warning, undefined index as they haven't been declared before being output.

3 True and good point, but should be a comment as it has nothing to do with the question – Pekka Aug 23 '10 at 14:09 Well I thought that breaking the whole code was to do with the question. – MRW Aug 23 '10 at 14:12 No, variable scope was to do with the question. Pekka is correct.

This is a very good and valid point, not an answer. – Stephen Aug 23 '10 at 14:13 Ok, fair point, my bad. – MRW Aug 23 '10 at 14:30.

Scope is the boundary of where you can access a variable (or property or method). Your code isn't an example of scope, it's syntax parsing. Within double quotes, php will recognize and try to evaluate variables.

Because $b.. is how you refer to an array element, php will try to parse it as such. Curly braces are used for multiple things. In the context of your code, they delimit the beginning and end if your if(...) condition, as in if (condition) { // do all // of this stuff // between the { and } // if the condition // is true } This has nothing to do with scope, unless you wanna look at it in the sense of "this is where the code to be executed if the condition is true starts and ends" but as mentioned, that's not what "scope" really means.

You can also use {..} to tell php where to start and end the variable name, to avoid ambiguity. For example: In this example, php will try to parse the variable as $abar because that is a valid variable name: $a = "foo"; $b = "$abar" echo $b; // output : nothing - $abar doesn't exist (will give you a notice) Since you want it to parse $a not $abar, you would use {..} to specify the beginning and end of the variable name: $a = "foo"; $b = "${a}bar" echo $b; // output : foobar.

Scope is the boundary of where you can access a variable (or property or method). Your code isn't an example of scope, it's syntax parsing. Within double quotes, php will recognize and try to evaluate variables.

Because $b.. is how you refer to an array element, php will try to parse it as such. Curly braces are used for multiple things. This has nothing to do with scope, unless you wanna look at it in the sense of "this is where the code to be executed if the condition is true starts and ends" but as mentioned, that's not what "scope" really means.

You can also use {..} to tell php where to start and end the variable name, to avoid ambiguity.

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