Home  >  Article  >  php教程  >  【基础】PHP变量及变量作用域 - wildfox

【基础】PHP变量及变量作用域 - wildfox

WBOY
WBOYOriginal
2016-05-20 10:13:141222browse

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

1. 变量的作用域

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

2. 变量的声明

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

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

3. 样例说明

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span>     
<span style="color: #008080;"> 3</span>     <span style="color: #800080;">$VarFile</span> = "Var_File";   <span style="color: #008000;">//</span><span style="color: #008000;">变量声明</span>
<span style="color: #008080;"> 4</span>     
<span style="color: #008080;"> 5</span>     <span style="color: #800080;">$nr</span> = <span style="color: #0000ff;">array</span>("\n","\r","\n\r","\r\n"<span style="color: #000000;">);
</span><span style="color: #008080;"> 6</span>     
<span style="color: #008080;"> 7</span>     
<span style="color: #008080;"> 8</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> TestClass
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">{    
</span><span style="color: #008080;">10</span>     <span style="color: #008000;">/*</span>
<span style="color: #008080;">11</span> <span style="color: #008000;">    *    分别用Var声明和private关键字声明
</span><span style="color: #008080;">12</span> <span style="color: #008000;">    *   $var = value; 这种不可以
</span><span style="color: #008080;">13</span> <span style="color: #008000;">    *    var private $var; 这种不可以
</span><span style="color: #008080;">14</span> <span style="color: #008000;">    *    var private $var = value; 这种不可以
</span><span style="color: #008080;">15</span> <span style="color: #008000;">    *    var $var; 这种不可以    
</span><span style="color: #008080;">16</span>     <span style="color: #008000;">*/</span>
<span style="color: #008080;">17</span>     
<span style="color: #008080;">18</span>     <span style="color: #0000ff;">var</span> <span style="color: #800080;">$VarClass</span> = 'VarClass declared by keyword Var'<span style="color: #000000;">;            
</span><span style="color: #008080;">19</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$VarClass2</span> = 'VarClass2 declared by keyword private'<span style="color: #000000;">;
</span><span style="color: #008080;">20</span>     
<span style="color: #008080;">21</span>     <span style="color: #008000;">/*</span>
<span style="color: #008080;">22</span> <span style="color: #008000;">    *    增加global关键字,使用global作用域的变量
</span><span style="color: #008080;">23</span> <span style="color: #008000;">    *   函数中声明变量方法与全局一样
</span><span style="color: #008080;">24</span>     <span style="color: #008000;">*/</span>
<span style="color: #008080;">25</span>     
<span style="color: #008080;">26</span>     <span style="color: #0000ff;">function</span><span style="color: #000000;"> GetAllVar() {
</span><span style="color: #008080;">27</span>         <span style="color: #0000ff;">global</span> <span style="color: #800080;">$VarFile</span><span style="color: #000000;">;
</span><span style="color: #008080;">28</span>         <span style="color: #800080;">$VarFunction</span> = 'Var_Function'<span style="color: #000000;">;
</span><span style="color: #008080;">29</span>         
<span style="color: #008080;">30</span>         <span style="color: #800080;">$Temp</span> = "<span style="color: #800080;">$VarFile</span>=".<span style="color: #800080;">$VarFile</span>."\r\n".
<span style="color: #008080;">31</span>                 "VarClass=".<span style="color: #800080;">$this</span>->VarClass."\r\n".
<span style="color: #008080;">32</span>                 "VarClass2=".<span style="color: #800080;">$this</span>->VarClass2."\r\n".
<span style="color: #008080;">33</span>                 "VarFunction=".<span style="color: #800080;">$VarFunction</span>."\r\n"<span style="color: #000000;">;
</span><span style="color: #008080;">34</span>         
<span style="color: #008080;">35</span>         
<span style="color: #008080;">36</span>         <span style="color: #0000ff;">return</span> <span style="color: #008080;">str_replace</span>(<span style="color: #800080;">$GLOBALS</span>['nr'],"<br>",<span style="color: #800080;">$Temp</span><span style="color: #000000;">);
</span><span style="color: #008080;">37</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">38</span>     
<span style="color: #008080;">39</span>     
<span style="color: #008080;">40</span> 
<span style="color: #008080;">41</span> <span style="color: #000000;">}
</span><span style="color: #008080;">42</span>     
<span style="color: #008080;">43</span>     <span style="color: #800080;">$MyClass</span> = <span style="color: #0000ff;">new</span> TestClass("zzy"<span style="color: #000000;">);
</span><span style="color: #008080;">44</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$MyClass</span>-><span style="color: #000000;">GetAllVar();
</span><span style="color: #008080;">45</span>     
<span style="color: #008080;">46</span> ?>

 

  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn