萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP變量的定義、可變變量、變量引用、銷毀方法

PHP變量的定義、可變變量、變量引用、銷毀方法

 這篇文章主要介紹了PHP變量的定義、可變變量、變量引用、銷毀方法,需要的朋友可以參考下

 代碼如下: <?php $long="big_long_variable_name"; $$long="PHP";     /* 用存放在變量$long裡的字符串作為新變量的變量名,等同於$big_long_variable_name="PHP"; */ $short=& $big_long_variable_name;  /* 取變量$big_long_variable_name的值賦給變量$short,此時$short的值為"PHP",等同於$short=& $$long; */ print "01 /$short is $short.";   /* "/$"是轉義序列,表示輸出一個美元符號$,下同。本語句的作用是輸出:01 $short is PHP. */ print "02 Long is $big_long_variable_name."; /* 輸出:02 Long is PHP. */ ?> <br /> <br /> <?php $big_long_variable_name.=" rocks!"; /* 重新對$big_long_variable_name賦值。重新賦值過程中,由於在$big_long_variable_name的後面添加了.(點號),因而變量$big_long_variable_name此時的值應為原值("PHP")+新值(" rocks!"),即變量$big_long_variable_name當前完整的值為"PHP rocks!"。下同。*/ print "03 /$short is $short";   /* 輸出:03 $short is PHP rocks! */ print "04 Long is $big_long_variable_name"; /* 輸出:04 Long is PHP rocks! */ ?> <br /> <br /> 05 $short is PHP rocks! 06 Long is PHP rocks! <br /> <br /> <?php $short.="Programming $short";   /* 重新對變量$short賦值。由於在$short後面添加了.(點號),因此請參考上例分析$short的值。*/ print "07 /$short is $short";   /* 輸出:07 $short is PHP rocks!Programming PHP rocks! */ print "08 Long is $big_long_variable_name"; /* 由於變量$short被重新賦值為Programming PHP rocks!,因而變量$big_long_variable_name的值也與$short一同被改變為"PHP rocks!Programming PHP rocks!"。本語句輸出:08 Long is PHP rocks!Programming PHP rocks!注意,如果是對具有相同值的一個變量進行銷毀unset( ),則另一個變量不適用於此種情況,即不會隨之被一同銷毀。*/ ?> <br /> <br /> 09 $short is Programming PHP rocks! 10 Long is Programming PHP rocks! <br /> <br /> <?php $big_long_variable_name.="Web Programming $short";  /* 變量$big_long_variable_name被重新賦值,此時它完整的值應為PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks!。變量$short的值此時與變量$big_long_variable_name一致。請分別參考第5處、第10處注釋進行分析。*/ print "11 /$short is $short";     /* 輸出:11 PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks! */ print "12 Long is $big_long_variable_name"; ?> <br /> <br /> <?php unset($big_long_variable_name);   /* 用unset( )銷毀變量$big_long_variable_name,變量$short不會因此受到任何影響。*/ print "13 /$short is $short";   /* 雖然銷毀了變量$big_long_variable_name,但$short沒有受到影響,它的值仍是最近一次被賦予的PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks! */ print "14 Long is $big_long_variable_name."; /* 變量$big_long_variable_name已被銷毀,故而無值。輸出:14 Long is. */ snow;                                     ?> <br /> <br /> <?php $short="No point TEST1";   /* 重新對變量$short賦值。由於這次沒有在$short後面添加.(點號),因此$short當前的值為"No point TEST1"。*/ print "15 /$short is $short.";   /* 輸出:15 $short is No point TEST1. */ $short="No point TEST2 $short";   /* 重新對變量$short賦值。沒在$short的後面添加.(點號),但引用了它自身最近一次的值"No point TEST1"。*/ print "16 /$short is $short.";   /* 輸出:16 $short is No point TEST2 No point TEST1. */  
copyright © 萬盛學電腦網 all rights reserved