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

WBOY
發布: 2016-07-12 08:52:31
原創
759 人瀏覽過

【基础】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> ?>
登入後複製

 

  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1126174.htmlTechArticle【基础】PHP变量及变量作用域,php变量作用域 新学PHP,比较有意思的语法,记录下。 1. 变量的作用域 作用域只分两个Global和Local,Global相...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板