Introduction: This is a detailed page on the scope of variables in IF and while. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 340514 'rolling = 'no'>
As we know, functions and classes change the scope of the current variable. If, while, and other branch loop structures inherit the external scope, that is, the external variables are visible inside the branch loop structure.
However, the C language does not support the internal scopes of if, while and other branch loop structures, whereas PHP does not.
In PHP, if, while, and other local variables declared in the branch loop structure are still valid after the branch loop structure.
For example, variables declared in a function are invisible outside the function.
C language:
# Include <stdio. h> int Foo () {int bar; bar = 3;} int main () {printf ("% d \ n", bar); Return 0 ;}
Error:
Scope. C: In function 'main ':
Scope. C: 11: Error: 'bar' is not declared (used for the first time in this function)
Scope. C: 11: Error: (even if it appears multiple times in a function, each undeclared identifier
Scope. C: 11: Error: only one report is reported in the function .)
PHP language:
<? Phpfunction Foo () {$ bar = 1;} echo $ bar;
Error:
Notice: undefined variable: bar in scope. php on line 7
PHP notice: undefined variable: bar in scope. php on line 7
The if, while, and other loop branch structures inherit the external scope, that is, the external variables are visible inside the loop branch structure.
Similar:
C language:
# Include <stdio. h> int main () {int Foo = 1; if (1) {int bar = Foo; printf ("% d \ n", bar);} while (1) {int bar = Foo; printf ("% d \ n", bar); break;} return 0 ;}
Output:
1
1
PHP language:
<? PHP $ Foo = 1; if (1) {$ bar = $ Foo; echo $ bar, "\ n" ;}while (1) {$ bar = $ Foo; echo $ bar, "\ n"; break ;}
Output:
1
1
But it's different.
C language:
# Include <stdio. h> int main () {int Foo = 1; if (1) {int bar = Foo; printf ("% d \ n", bar );} printf ("% d \ n", bar); While (1) {int bar = Foo; printf ("% d \ n", bar); break ;} printf ("% d \ n", bar); Return 0 ;}
Error:
Scope. C: In function 'main ':
Scope. C: 12: Error: 'bar' is not declared (used for the first time in this function)
Scope. C: 12: Error: (even if it appears multiple times in a function, each undeclared identifier
Scope. C: 12: Error: only one report is reported in the function .)
PHP language
<? PHP $ Foo = 1; if (1) {$ bar = $ Foo; echo $ bar ++, "\ n";} echo $ bar, "\ n "; while (1) {$ bar2 = $ Foo; echo $ bar2 ++, "\ n"; break;} echo $ bar2, "\ n ";
Output:
1
2
1
2
This is what you need to pay attention!
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/340514.html pageno: 7.