[Basics] PHP variables and variable scopes, php variable scopes
New PHP, more interesting syntax, record.
1. Scope of Variables
The scope can only be divided into two Global and Local. Compared with the entire. php file, Global is the smallest Local range and the closest range to the variable. For example, in a function, the class is medium.
2. Variable Declaration
It may be because the PHP variable has a unique $ symbol, so it does not need keywords to declare the PHP variable (except in the class). It is automatically created when the first value is assigned.
The class is quite unique. Because the class has the member attributes private, public, and protected, you need to modify keywords when declaring variables in the class. The preceding keyword or var is used, but the two cannot be shared.
3. Example
1 <? Php 2 3 $ VarFile = "Var_File"; // variable declaration 4 5 $ nr = array ("\ n", "\ r", "\ n \ r ", "\ r \ n"); 6 7 8 class TestClass 9 {10/* 11 * declare 12 * $ Var = value with the var and private keywords respectively; this type cannot be 13 * var private $ var; this type cannot be 14 * var private $ var = value; this type cannot be 15 * var $ var; this type cannot be 16 */17 18 var $ VarClass = 'varclass declared by keyword var'; 19 private $ VarClass2 = 'varclass2 declared by keyword private '; 20 21/* 22 * Add the global keyword, use g The variable declaration method in the variable 23 * function in the lobal scope is the same as that in the global 24 */25 26 Function GetAllVar () {27 global $ VarFile; 28 $ VarFunction = 'var _ function '; 29 30 $ Temp = "$ VarFile = ". $ VarFile. "\ r \ n ". 31 "VarClass = ". $ this-> VarClass. "\ r \ n ". 32 "VarClass2 = ". $ this-> VarClass2. "\ r \ n ". 33 "VarFunction = ". $ VarFunction. "\ r \ n"; 34 35 36 return str_replace ($ GLOBALS ['nr '], "<br/>", $ Temp ); 37} 38 39 40 41} 42 43 $ MyClass = new TestClass ("inline"); 44 echo $ mycia Ss-> GetAllVar (); 45 46?>