【基础】PHP变量及变量作用域,php变量作用域_PHP教程

WBOY
Libérer: 2016-07-12 08:52:31
original
758 Les gens l'ont consulté

【基础】PHP变量及变量作用域,php变量作用域

   新学PHP,比较有意思的语法,记录下。

1. 变量的作用域

   作用域只分两个Global和Local,Global相对于整个.php文件来讲,Local是本地最小范围,是距离变量最近的范围,如:在函数中,在类中等。

2. 变量的声明

    可能是由于PHP变量独特的$符号,所以声明PHP变量不需要关键字(类中除外),第一次赋值的时候自动创建了。

    类中却比较独特了,由于类带有成员属性private、public、protected,那么在类中声明变量的时候是需要关键字修饰的。用前面所说的关键字或这个关键字var,但是不可以二者共用。

3. 样例说明

<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> ?>
Copier après la connexion

 

  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1126174.htmlTechArticle【基础】PHP变量及变量作用域,php变量作用域 新学PHP,比较有意思的语法,记录下。 1. 变量的作用域 作用域只分两个Global和Local,Global相...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal