";"/> ";">
php:函數間的參數傳遞
#1.值傳遞
<?php function exam($var1){ $var1++; echo "In Exam:" . $var1 . "<br />"; } $var1 = 1; echo $var1 . "<br />"; exam($var1); echo $var1 . "<br />"; ?>
------------------------------------------------- ------------------------------
輸出結果:
1
In Exam: 2
1
---------------------------------------------------------------- ---------------------------------
2.引用傳遞
<?php function exam( &$var1){ $var1++; echo "In Exam:" . $var1 . "<br />"; } $var1 = 1; echo $var1 . "<br />"; exam($var1); echo $var1 . "<br />"; ?>
----------------------------------------- --------------------------------------
輸出結果:
1
In Exam: 2
2
-------------------------------------- ------------------------------------------
3.可選參數
function values($price, $tax=""){ $price += $prive * $tax; echo "Total Price:" . $price . "<br />"; } values(100, 0.25); values(100);
輸出結果:
Total Price: 125
Total Price: 100
------------------ -------------------------------------------------- -----------
4.如果傳入的是一個物件,可以改變該物件的值
(實際上變數$obj記錄的是這個物件的句柄,將$obj當作參數傳入,完全可以對原始物件進行操作。 -------------------------------------------------- ------------------
Mr. zhan 12 male
Mr. zhan 13 male
Mr. zhan 14 male
以上是php自訂函數間的值傳遞/引用傳遞用法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!