Newly learned PHP, interesting syntax, record it.
1. Scope of variables
The scope is only divided into two parts: Global and Local. Global is relative to the entire .php file, and Local is the local minimum scope, which is the closest scope to the variable, such as: in a function, in a class, etc.
2. Declaration of variables
It may be due to the unique $ symbol of PHP variables, so no keywords are required to declare PHP variables (except in classes), and they are automatically created the first time they are assigned.
The class is quite unique. Since the class has member attributes private, public, and protected, keyword modification is required when declaring variables in the class. Use the keyword mentioned earlier or the keyword var, but not both.
3. Sample description
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>$VarFile</span> = "Var_File"; <span>//</span><span>变量声明</span> <span> 4</span> <span> 5</span> <span>$nr</span> = <span>array</span>("\n","\r","\n\r","\r\n"<span>); </span><span> 6</span> <span> 7</span> <span> 8</span> <span>class</span><span> TestClass </span><span> 9</span> <span>{ </span><span>10</span> <span>/*</span> <span>11</span> <span> * 分别用Var声明和private关键字声明 </span><span>12</span> <span> * $var = value; 这种不可以 </span><span>13</span> <span> * var private $var; 这种不可以 </span><span>14</span> <span> * var private $var = value; 这种不可以 </span><span>15</span> <span> * var $var; 这种不可以 </span><span>16</span> <span>*/</span> <span>17</span> <span>18</span> <span>var</span> <span>$VarClass</span> = 'VarClass declared by keyword Var'<span>; </span><span>19</span> <span>private</span> <span>$VarClass2</span> = 'VarClass2 declared by keyword private'<span>; </span><span>20</span> <span>21</span> <span>/*</span> <span>22</span> <span> * 增加global关键字,使用global作用域的变量 </span><span>23</span> <span> * 函数中声明变量方法与全局一样 </span><span>24</span> <span>*/</span> <span>25</span> <span>26</span> <span>function</span><span> GetAllVar() { </span><span>27</span> <span>global</span> <span>$VarFile</span><span>; </span><span>28</span> <span>$VarFunction</span> = 'Var_Function'<span>; </span><span>29</span> <span>30</span> <span>$Temp</span> = "<span>$VarFile</span>=".<span>$VarFile</span>."\r\n". <span>31</span> "VarClass=".<span>$this</span>->VarClass."\r\n". <span>32</span> "VarClass2=".<span>$this</span>->VarClass2."\r\n". <span>33</span> "VarFunction=".<span>$VarFunction</span>."\r\n"<span>; </span><span>34</span> <span>35</span> <span>36</span> <span>return</span> <span>str_replace</span>(<span>$GLOBALS</span>['nr'],"<br/>",<span>$Temp</span><span>); </span><span>37</span> <span> } </span><span>38</span> <span>39</span> <span>40</span> <span>41</span> <span>} </span><span>42</span> <span>43</span> <span>$MyClass</span> = <span>new</span> TestClass("zzy"<span>); </span><span>44</span> <span>echo</span> <span>$MyClass</span>-><span>GetAllVar(); </span><span>45</span> <span>46</span> ?>