PHP ??
??? ??? ???? ? ???? "????"???.
??? ?????. ???? ??. ????? ?? ??? ??? ????.
?????, Li Lei? Han Meimei, if:
x = 5
y = 6
then x + y? ?????? ?? ?? ?? ???? ???. x + y? 11???.
???? ?? ??? ?? ??? ???????. x + y? ??? ??????
x = 5
y = 6
x = 8
??? ?? ? ??? ???? ?? ??: x + y? ??? 14???.
?????!
??? ?? ??:
1. = 5 ???? ? 5? ??? x
2? ???? ? ?? ?? x = 8?? x + y? ?? ??? 14? ???? x? ??? ??? ?????. ??) ??? ??? ?? ??? ? ????.
PHP ??
PHP? ?? ?? ????. ??? ??? ? ?? ????:
1. $?? ???? ???. ?? ?? ?? ??, ???, _? ?? ??? ????
5. ?? ??? ??? ??? ???(xxx, aaa, ccc? ?? ?? ??? ?? ????)
6. $? ??? ?? ??(dollar sign)?? ???. PHP ??? ?? ??? ???? ???. PHP? ??? ?? ??? "?"?? ???? ??
PHP ?? ??(??)
PHP?? ??? ???? ??? ????. ?? ?? ???? ??? ?????.
<?php $txt="Hello world!"; $x=5; $y=10.5; ?>
? ??? ???? txt ??? Hello world! ?? ???? ?? x? ? 5 ? ??????.
??
: ??? ??? ?? ??? ? ??? ? ??? ???? ????.
PHP? ?? ??? ?????.
?? ???, ??? ? ??? ??? ??? PHP? ??? ??? ??? ?? ?? ????.
PHP? ??? ?? ?? ??? ??? ??? ???? ?? ?????.
??? ??? ????? ????? ??? ???? ?? ??? ??? ??? ??(??)?? ???.
PHP ?? ??
??? ??? ??? ??? ? ?? ???? ?????. /???.
PHP?? ? ?? ?? ??? ????.
· ??
· ???
· ???
· ????
?? ? ??? ?? ?? ? ??? ??(local global)
?? ?? ???? ??? ??? ?? ??? ????. ?? ??? ?? ??? ????? ?? ???? ???? ? ????. ??? ?? ??? ?????? global ???? ???? ???.
????
PHP ?? ??? ??? ??? ?? ???? PHP ?? ????? ??? ? ????. ?? ??:
<?php header("Content-type:text/html;charset=utf-8"); $x=5; // 全局變量 function myTest() { $y=10; // 局部變量 echo "<p>測(cè)試函數(shù)內(nèi)變量:<p>"; echo "變量 x 為: $x"; //輸出錯(cuò)誤 Notice: Undefined variable: echo "<br>"; echo "變量 y 為: $y"; } myTest(); echo "<p>測(cè)試函數(shù)外變量:<p>"; echo "變量 x 為: $x"; echo "<br>"; echo "變量 y 為: $y"; //輸出錯(cuò)誤 Notice: Undefined variable:
?? ??? myTest() ??? $x ? $y ??? ?????. $x ??? ?? ???? ??????? ?? ????, $y ??? ?? ???? ?????
?? ?????.
myTest() ??? ???? ? ??? ?? ???? ??? ?? ?? $y? ?? ????? $x ??? ???? ????? $x? ?? ??? ? ????. ??
?? ??? ??? ? ????. ?? ??? ?? ??? ????? global ???? ???? ???.
?? ?? myTest() ?? ??? ? ??? ?? ?????. ??? ?? ?? ?? $x? ?? ????? $y? ?? $y? ??? ? ????. ???
?? ??? ??? ?? ??? ????.
?? ??? ??? ??? ?? ??? ?? ???? ?? ?? ???? ????? ?? ???? ??? ?? ??? ??? ? ????.
PHP ??? ???
??? ??? ?? ?? ?? ??? ????? ? ?????.
?
?? ??? ?? ??? ??? ?? ??? ????? ?? ?? ?? ?? ???? ???? ???. ??:
<?php $x=10; $y=20; function test(){ global $x,$y; //使用global關(guān)鍵字 $y=$x+$y; } test(); echo $y;//輸出30
PHP? ?? ?? ??? $GLOBALS[index]?? ??? ?????. index? ??? ??? ?? ????. ? ??? ?? ??? ???? ? ???
? ???? ?? ??? ?? ????? ? ????.
?? ?? ??? ?? ??? ? ????.
<?php $x=5; $y=10; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?>
Static ??
??? ???? ????? ?? ??? ?????. ??? ??? ?? ??? ???? ??? ?? ?? ????.
??? ??? ??? ?? ??? ? static ???? ?????.
Instance
<?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); myTest(); ?>
??? ??? ??? ??? ??? ??? ??? ????? ?? ?? ?????.
?? : ? ??? ??? ??? ?? ?????.
???? ??
????? ?? ??? ?? ??? ?? ???? ?? ?????.
????? ?? ??? ??? ???? ??? ?????.
????
?php function myTest($x) { echo $x; } myTest(name); ?>
?? ???? PHP ?? ??? ? ??? ???????.