PHP ?? ?? ????? ??
??? ??? ???? ? ???? "????"???.
?? ? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> </body> </html>
1. ???? ??
x=5
y=6
z=x+y
?????? ??(?: x)? ???? ?(?: 5)? ?????.
? ? z=x+y?? z? ?? 11?? ??? ? ????.
PHP??? ? ? ??? ???? ???.
??: ??? ???? ???? ?????? ?????.
2. PHP ??
???? ????? PHP ??? ?? ??? ? ????.(x=5) ? ???(z =x+y).
?? ??? ?? ?? ??(?: x ? y) ? ???? ?? ????(?: ??, total_volume).
3. PHP ?? ??
??? $ ??? ???? ? ?? ?? ??? ???.
?? ??? ??? ??? ???? ???.
?? ??? ??? ??? ? ????.
?? ??? ???? ??? ? ????. ?? ?? ?? ? ??(A-z, 0-9 ? _)
?? ??? ????? ?????($y? $Y? ?? ?? ?????)
??: PHP ?? ??? ????? ?????!
?:
<?php //site = 'Hello'; // 非法變量名;以數(shù)字開頭 $_4site = 'World'; // 合法變量名;以下劃線開頭 $i小明is = 'haha'; // 合法變量名;可以用中文 ?>
4. PHP ?? ???
PHP?? ????. ?? ?? ??.
?? ?? ???? ??? ?????.
<?php $txt="Hello world!"; $x=5; $y=10.5; ?>
? ??? ???? txt ??? Hello world! ?? ???? ?? x? ? 5 ? ??????.
??: ??? ??? ?? ??? ? ??? ? ??? ???? ?????.
5. PHP? ??? ??? ?????
?? ???? PHP? ??? ??? ??? ??????. ??? ??? ??? ?????.
PHP? ?? ?? ?? ??? ??? ??? ???? ?? ?????.
??? ??? ????? ????? ??? ???? ?? ??? ??? ??? ??(??)?? ???.
6. PHP ?? ??(???? ??? ???? ?? ????? ???? ?? ????)
?? ?? ? ????? ?? ??? ??/??? ? ?? ?????.
PHP?? ? ?? ?? ??? ????.
??
???
static
????
1. ?? ? ?? ??
??? ?????. ??? ?? ???? ?? ??? ????. ?? ??? ?? ??? ????? ?? ???? ???? ? ????. ??? ?? ??? ?????? global ???? ???? ???.
PHP ?? ??? ??? ??? ?? ???? ?? ????? ???? ? ????:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; // 全局變量 function myTest() { $y=10; // 局部變量 echo "<p>測試函數(shù)內(nèi)變量:<p>"; echo "變量 x 為: $x"; echo "<br>"; echo "變量 y 為: $y"; } myTest(); echo "<p>測試函數(shù)外變量:<p>"; echo "變量 x 為: $x"; echo "<br>"; echo "變量 y 為: $y"; ?> </body> </html>
2.PHP ?? ???
?? ???? ?? ?? ?? ??? ????? ? ?????.
?? ??? ?? ??? ??? ?? ??? ????? ??? ?? ?? ?? ???? ???? ???.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; $y=6; function test(){ global $x,$y; $y=$x+$y; } test(); echo $y; ?> </body> </html>
??: ??? ???? ??? ???? ??? ? ????.
PHP? ?? ?? ??? $GLOBALS[index]?? ??? ?????. index? ??? ??? ?? ????. ? ??? ?? ??? ?????? ?? ??? ?????? ? ?? ??? ? ????.
?? ?? ??? ?? ??? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; $y=6; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); myTest(); myTest(); myTest(); echo $y; ?> </body> </html>
??: ? ??? ?? ??? ?????
3 .PHP ?? ???
??? ???? ????? ?? ??? ?????. ??? ??? ?? ??? ???? ??? ?? ?? ????.
??? ??? ??? ?? ??? ? static ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); ?> </body> </html>
??: global ??? ???? ?????? ? ?
4. ???? ??
????? ?? ??? ?? ??? ?? ???? ?? ?????.
????? ?? ??? ??? ???? ??? ?????.
??? ??? PHP ?? ?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php function test($x) { echo $x; } test(2016); ?> </body> </html>
?????. ??>
7. ?? ??
<?php $x= 'hello'; $$x='xiao ming'; echo "$x ${$x}"; ?>? ??? ??? ?? ??? ?? ????.
<?php $x= 'hello'; $hello='xiao ming'; echo "$x $hello"; ?>? ? ??? ?????. ??: ?? ??? ??? ???? ??? ??? ???? ???. ?? $$a[1]? ??? ? ??? $a[1]? ??? ????, ??? $$a? ??? ???? ??? ?? ??? [1] ??? ??? ???? ???. ? ??? ???? ?? ??? ? ?? ???? ${$a[1]}? ???? ? ?? ???? ${$a}[1]? ???? ????.
?? ??:
??? ?? ??, ??? ??? ?????
? ?? ?? ??? ??? ???