<? = "why" = "what"changeName(); "my name is " . . "<br/>"?>
執行代碼的結果是:my name is why,而不是執行changeName()後顯示what。分析原因,這是因為函數體changeName內$name變量被缺省設置為局部變量,$name的作用域就是在changeName內。於是,修改代碼,添加全局變量如下:
<? = "why" = "what"changeName(); "my name is " . . "<br/>"?>
注意:
定義全局變量後執行的結果仍然為my name is why,這個結果讓我吃驚。原來," 也就是說,當一個函數引用一個外部變量時,可以在函數內通過global來聲明該變量,這樣該變量就可以在函數中使用了(相當於當作參數傳遞進來)。那麼,再次修改代碼:
<? = "why" = "what" "my name is " . . "<br/>"?>
這次的運行結果是:my name is what,說明
<? = "why" = "what" = "where" "my name is " . . "<br/>"?>
<? = "why"['name'] = "what" "my name is " . . "<br/>"?>
<? = 1 = 2['var2'] = &['var1' . "<br />" = 1 = 2 , = & . "<br />"?>
; 就可以訪問它們。與所有其他超全局變量不同,$GLOBALS在PHP中總是可用的。另一個例子:
<? = 1(['var1' . "<br />" = 1 ( ?>