首頁 >後端開發 >php教程 >php函數間的參數傳遞(值傳遞/引用傳遞)_PHP教程

php函數間的參數傳遞(值傳遞/引用傳遞)_PHP教程

WBOY
WBOY原創
2016-07-21 16:12:49809瀏覽

php:函數間的參數傳遞

1.值傳遞

複製程式碼 程式碼如下:

function exam($var1){
$var1 ;
echo "In Exam:" . $var1 . "
";
}

$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
? >

---------------------------------------- ---------------------------------------
輸出結果:
1
In Exam: 2
1
------------------------------------- ------------------------------------------
2.引用傳遞
複製程式碼 程式碼如下:

function exam($var1) {
$var1 ;
echo "In Exam:" . $var1 . "
";
}

$var1 = 1;
echo $var1 . "
$var1 = 1;
echo $var1 . "
";
exam($var1);
echo $var1 . "
";
?>



---- -------------------------------------------------- -------------------------
輸出結果:
1
In Exam: 2
2 - -------------------------------------------------- ----------------------------
3.選用參數
複製程式碼
程式碼如下:


function values($price, $tax=""){
$price = $prive * $tax;
echo "Total Price:" . $price . "
";
}

values(100, 0.25);
values(100);


輸出結果:
Total Price: 125
Total Price: 100 --------------------------- -------------------------------------------------- --
4.如果傳入的是一個對象,可以更改該對象的值
(實際上變數$obj記錄的是這個對象的句柄,將$obj作為參數傳入,完全可以對原物件進行操作。 ) 複製程式碼
程式碼如下:


class Obj{
public $name; 🎜>public $age;
public $gander;
public function __construct($name, $age, $gander){
$this->name = $name;
$this->age = $age;
$this->gander = $gander;
}
public function show_info(){
echo $this->name . " " . $this->age . " " . $this->gander . "
";
}
}
function grow($obj){
$obj->age ;
}
function test (){
$obj = new Obj("Mr. zhan", "12", "male");
$obj->show_info();
grow($obj);
$obj->show_info();
grow($obj);
$obj->show_info();
}
test();
?>


----------------------------------------------- --------------------------------
輸出結果:
Mr. zhan 12 male
Mr . zhan 13 male Mr. zhan 14 male

http://www.bkjia.com/PHPjc/313595.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313595.htmlTechArticlephp:函數間的參數傳遞1.值傳遞複製程式碼如下: ?php function exam($var1) { $var1 ; echo "In Exam:" . $var1 . "br /"; } $var1 = 1; echo $var1 . "br /"; exam($v...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn