變數是用來儲存資料的容器,而與代數相似,可以給予變數某一個確定的值(例如:$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>(php學習日誌(2)-php變數,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主要有以下四種變數作用域:
(php學習日誌(2)-php變數)局部與全局作用域
在函數外定義的變量,擁有全域作用域,除了函數外,全域作用域可以被腳本中的任何部分訪問,要在一個函數中訪問全域變量,需要在函數中變量之前加上
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>=php學習日誌(2)-php變數0<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=php學習日誌(2)-php變數php學習日誌(2)-php變數</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_php學習日誌(2)-php變數() { </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']=php學習日誌(2)-php變數5<span style="color: #000000">; } </span><span style="color: #0000ff">echo</span> "执行myTest_php學習日誌(2)-php變數:<br>"<span style="color: #000000">; myTest_php學習日誌(2)-php變數(); </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']=php學習日誌(2)-php變數2; <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>=php學習日誌(2)-php變數<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">输出为:php學習日誌(2)-php變數</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新手,有不对的地方希望各位博友提醒,万分感谢==