变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变量(例如:$x=$y+$z)。变量的定义主要有以下规则:
php中的变量在第一次赋予它值的时候被创建,如果变量未赋值,则在输出时会出现错误,如下面所示:
<span style="color: #000000">php </span><span style="color: #800080">$x</span><span style="color: #000000">; </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$x</span><span style="color: #000000">; </span>?>登录后复制
此时浏览器会出现错误提示:
这个部分与Python有所区别,Python中如果变量没有被赋值,编译器会直接提示错误,我们需要注意一下。
图、Python变量未赋值发生错误
php是一门弱类型语言,在定义变量时,我们不需要定义变量的类型,php会根据变量的值,自动的将变量转换成正确的数据类型。如下例所示:
<span style="color: #000000">php </span><span style="color: #800080">$x</span>=3<span style="color: #000000">; </span><span style="color: #800080">$y</span>=3.0<span style="color: #000000">; </span><span style="color: #800080">$str</span>="hello"<span style="color: #000000">; </span><span style="color: #800080">$bool</span>=<span style="color: #0000ff">false</span><span style="color: #000000">; </span><span style="color: #800080">$arr</span>=<span style="color: #0000ff">array</span>(1,2,3<span style="color: #000000">); </span><span style="color: #800080">$_null</span>=<span style="color: #0000ff">NULL</span><span style="color: #000000">; </span><span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$x</span>),"<br>"; <span style="color: #008000">//</span><span style="color: #008000">输出类型为integ</span> <span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$y</span>),"<br>"; <span style="color: #008000">//</span><span style="color: #008000">输出类型为double</span> <span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$str</span>),"<br>"; <span style="color: #008000">//</span><span style="color: #008000">输出类型为string</span> <span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$bool</span>),"<br>"; <span style="color: #008000">//</span><span style="color: #008000">输出类型为boolean</span> <span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$arr</span>),"<br>"; <span style="color: #008000">//</span><span style="color: #008000">输出类型为array</span> <span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$_null</span>),"<br>"; <span style="color: #008000">//</span><span style="color: #008000">输出类型为NULL</span> ?>
结果为:
接下来我们将对php的四种变量作用域进行讨论,变量作用域定义了变量的作用范围,php主要有以下四种变量作用域:
(1)局部与全局作用域
在函数外定义的变量,拥有全局作用域,除了函数外,全局作用域可以被脚本中的任何部分访问,要在一个函数中访问全局变量,需要在函数中变量之前加上
global 关键字。
实例:
<span style="color: #000000">php </span><span style="color: #008000">/*</span><span style="color: #008000">局部作用域与全局作用域</span><span style="color: #008000">*/</span> <span style="color: #800080">$a</span>=5<span style="color: #000000">; </span><span style="color: #0000ff">function</span><span style="color: #000000"> test() { </span><span style="color: #800080">$b</span>=10<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> "测试函数内变量<br>"<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> "变量a的值为:<span style="color: #800080">$a</span> <br>"; <span style="color: #008000">//</span><span style="color: #008000">变量$a未在函数内定义,在引用时出现错误.</span> <span style="color: #0000ff">echo</span> "变量b的值为:<span style="color: #800080">$b</span><br>"<span style="color: #000000">; } test(); </span><span style="color: #0000ff">echo</span> "测试函数外变量<br>"<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> "变量a的值为:<span style="color: #800080">$a</span> <br>"<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> "变量b的值为:<span style="color: #800080">$b</span><br>"; <span style="color: #008000">//</span><span style="color: #008000">变量$a未在函数内定义,在引用时出现警告.</span> ?>
结果如下:
图、局部变量与全局变量
可见,在局部函数里面,是不能直接访问全局变量的,如果要访问全局变量,须在函数里面的变量前加上global关键字。同样,在函数外也不能直接访问函数里面的变量,函数执行结束内存会自动回收,故我们无法访问到。
(2)global关键字
global 关键字用于函数内访问全局变量,在函数内调用函数外定义的全局变量,需要用global关键字。值得一提的是,这和参数的调用不同,参数的调用并不会改变该变量在内存中的值,而global调用是直接调用内存中的该值,直接对它进行操作,故会改变其值。
<span style="color: #000000">php </span><span style="color: #008000">/*</span><span style="color: #008000">在函数内调用函数外的变量,需要用到global关键字</span><span style="color: #008000">*/</span> <span style="color: #008000">/*重要:</span><span style="color: #008000">这种调用会改变变量在内存中的值</span><span style="color: #008000">*/</span> <span style="color: #800080">$x</span>=5<span style="color: #000000">; </span><span style="color: #800080">$y</span>=6<span style="color: #000000">; </span><span style="color: #008000">/*</span><span style="color: #008000"> * 以下这种方式是不被允许的,只有在函数内调用函数外的变量才能使用global golbal $z=7; </span><span style="color: #008000">*/</span> <span style="color: #0000ff">function</span><span style="color: #000000"> myTest() { </span><span style="color: #0000ff">global</span> <span style="color: #800080">$x</span>,<span style="color: #800080">$y</span><span style="color: #000000">; </span><span style="color: #800080">$y</span>=<span style="color: #800080">$x</span>+<span style="color: #800080">$y</span><span style="color: #000000">; } myTest(); </span><span style="color: #0000ff">echo</span> "y=<span style="color: #800080">$y</span>" <span style="color: #008000">//</span><span style="color: #008000">输出y=11</span> ?>
*注意:超级全局变量 $GLOBALS[index]
php将所有全局变量存储在一个名为:$GLOBALS[index]的数组中,这个数组可以在函数内访问,也可以用来直接更新全局变量(只能在函数内进行)。
实例:
<span style="color: #000000">php </span><span style="color: #008000">/*</span><span style="color: #008000"> GLOBALS[index]的用法讲解 </span><span style="color: #008000">*/</span> <span style="color: #800080">$x</span>=5<span style="color: #000000">; </span><span style="color: #800080">$y</span>=6<span style="color: #000000">; </span><span style="color: #0000ff">function</span><span style="color: #000000"> myTest_1() { </span><span style="color: #800080">$GLOBALS</span>['y']=<span style="color: #800080">$GLOBALS</span>['x']+<span style="color: #800080">$GLOBALS</span>['y'<span style="color: #000000">]; </span><span style="color: #008000">/*</span><span style="color: #008000">index不用写$字符,否则报错:$x,$y undefined $GLOBALS['$y']=$GLOBALS['$x']+$GLOBALS['$y']; </span><span style="color: #008000">*/</span><span style="color: #000000"> } </span><span style="color: #0000ff">function</span><span style="color: #000000"> myTest_2() { </span><span style="color: #800080">$GLOBALS</span>['x']=15<span style="color: #000000">; } </span><span style="color: #0000ff">echo</span> "执行myTest_1:<br>"<span style="color: #000000">; myTest_1(); </span><span style="color: #0000ff">echo</span> "y=<span style="color: #800080">$y</span>","<br>"<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">; </span><span style="color: #800080">$GLOBALS</span>['$x']=12; <span style="color: #008000">//</span><span style="color: #008000">未将GLOBALS[index]放在函数里面,无效</span> <span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> "执行myTest_2:<br>"<span style="color: #000000">; myTest_2(); </span><span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">; </span>?>
结果为:
图、GLOBALS[index]的用法示例
(3)static关键字
在一个函数执行完成之后,它的变量通常都会删除,有时我们希望函数中的某个变量保留,这时我们可以在申明变量时使用static关键字:
实例:
<span style="color: #000000">php </span><span style="color: #008000">/*</span><span style="color: #008000">静态变量static的用法</span><span style="color: #008000">*/</span> <span style="color: #0000ff">function</span><span style="color: #000000"> myTest() { </span><span style="color: #0000ff">static</span> <span style="color: #800080">$x</span>=0,<span style="color: #800080">$y</span>=1<span style="color: #000000">; </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$x</span><span style="color: #000000">; </span><span style="color: #800080">$x</span>++<span style="color: #000000">; } myTest(); </span><span style="color: #008000">//</span><span style="color: #008000">输出为:0</span> myTest(); <span style="color: #008000">//</span><span style="color: #008000">输出为:1</span> myTest(); <span style="color: #008000">//</span><span style="color: #008000">输出为:2</span> <span style="color: #008000">/*</span><span style="color: #008000">函数里面的静态变量不能直接被访问</span><span style="color: #008000">*/</span> <span style="color: #0000ff">echo</span> <span style="color: #800080">$y</span>; <span style="color: #008000">//</span><span style="color: #008000">输出为:Notice: Undefined variable: y</span> ?>
(4)参数作用域
参数(parameter)的作用是将值传递给函数的局部变量。
实例:
<span style="color: #000000">php </span><span style="color: #008000">/*</span><span style="color: #008000">参数(parameter)传递</span><span style="color: #008000">*/</span> <span style="color: #0000ff">function</span> myTest(<span style="color: #800080">$x</span><span style="color: #000000">) { </span><span style="color: #0000ff">echo</span> "传递的值为:<span style="color: #800080">$x</span>.<br>"<span style="color: #000000">; } myTest(</span>5); <span style="color: #008000">//</span><span style="color: #008000">结果为:传递的值为5.</span> myTest("string") <span style="color: #008000">//</span><span style="color: #008000">结果为:传递的值为string.</span> ?>
==php新手,有不对的地方希望各位博友提醒,万分感谢==