4,250
社区成员
发帖
与我相关
我的任务
分享
<?php
/**
*引用和传值的例子
*
*author:Dave Ning
*/
//==========functions=============================================
//内部采用引用处理
function TestRefer($a)
{
foreach($a as &$b)
{
$b = 'dog';
}
dumpArray($a, 'the demo (in refer function) is :');
}
//内部采用值处理
function TestValue($a)
{
foreach($a as $b)
{
$b = 'dog';
}
dumpArray($a, 'the demo (in value function) is :');
}
//输出数组
function dumpArray($arr, $str='')
{
echo($str);
print_r($arr);
echo('<br />');
}
//=============function end================================
// variant reference
$a = 'aaaa';
$b = &$a;
$b = 'bbbbb';
echo('$a is ' . $a . '<br />');
$c = 'cccccc';
$d = $c;
$d = 'ddddd';
echo('$c is ' . $c . '<br />');
//function reference
$demo = array('a' => 'apple',
'b' => 'orange',
'c' => 'banana');
dumpArray($demo, 'the demo is :');
//传值
TestRefer($demo);
dumpArray($demo, 'the demo (pass value to refer fun)is :');
//传引用
TestRefer(&$demo);
dumpArray($demo, 'the demo (pass reference to refer fun)is :');
//传值
TestValue($demo);
dumpArray($demo, 'the demo (pass value to value fun)is :');
//传引用
TestValue(&$demo);
dumpArray($demo, 'the demo (pass reference to value fun)is :');
?>