|
在php中編寫(xiě)函數(shù),一般情況下調(diào)用函數(shù)的時(shí)候,改變的值都是形參而不是實(shí)參.但是如果在形參中加入地址符時(shí)候就會(huì)改變實(shí)參的值,為什么?
請(qǐng)看下面的例子:
復(fù)制代碼 代碼如下:
<?php
//編寫(xiě)一個(gè)函數(shù)swap(),測(cè)試該函數(shù)的實(shí)參值無(wú)改變
function swap($a,$b) {
echo "<p>進(jìn)入swqp()函數(shù)前<br>/n";
echo "交換前:形參a=$a,形參b=$b<br>/n";
$c=$b;
$a=$b;
$b=$c;
echo "交換后:形參a=$a,形參b=$b<br>/n";
echo "退出swap()函數(shù)<br></p>/n";
}
$variablea=5;
$variableb=10;
echo "調(diào)用swap()函數(shù)前: ";
echo "實(shí)參a=$variablea,實(shí)參b=$variableb<br>/n";
swap($variablea,$variableb);
echo "調(diào)用swap()函數(shù)后: ";
echo "實(shí)參a=$variablea,實(shí)參b=$variableb<br>/n";
?>
復(fù)制代碼 代碼如下:
<?php
//測(cè)試swap()函數(shù)實(shí)參的值改變
function swap1(&$a,&$b) {
echo "<p>進(jìn)入swap1()函數(shù)<br>/n";
echo "交換前: 形參a=$a,形參b=$b<br>/n";
$c=$b;
$a=$b;
$b=$c;
echo "交換后: 形參a=$a,形參b=$b<br>/n";
echo "退出swap()函數(shù)<br></p>/n";
}
$variablea=5;
$variableb=10;
echo "調(diào)用swap1()函數(shù)前: ";
echo "實(shí)參a=$variablea,實(shí)參b=$variableb<br>/n";
swap1($variablea,$variableb);
echo "調(diào)用swap1()函數(shù)后: ";
echo "實(shí)參a=$variablea,實(shí)參b=$variableb<br>/n";
?>
//上面的兩個(gè)例子就是說(shuō)明,求教了~~~
php技術(shù):php中函數(shù)的形參與實(shí)參的問(wèn)題說(shuō)明,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。