Home > Backend Development > PHP Tutorial > 程序员 - PHP自定义函数前加&,为什么效果一样呢,上全部代码

程序员 - PHP自定义函数前加&,为什么效果一样呢,上全部代码

WBOY
Release: 2016-06-06 20:46:34
Original
1199 people have browsed it

$a = 'wap';
$b ='kkk';

<code>function sum($p)
{
     return $p.='aaa';
}

function &sum2($p)
{
    return $p.='bbb';
}



 echo sum($a);
 echo "<br>";
 echo sum2($b);
</code>
Copy after login
Copy after login

回复内容:

$a = 'wap';
$b ='kkk';

<code>function sum($p)
{
     return $p.='aaa';
}

function &sum2($p)
{
    return $p.='bbb';
}



 echo sum($a);
 echo "<br>";
 echo sum2($b);
</code>
Copy after login
Copy after login

我已经在你的另一个问题中回答了你的相关提问,在此例中,如果想进行比较,你可以将代码修改为:

<code>$a = 'wap';
$b ='kkk';

function sum($p)
{
     return $p.='aaa';
}

function &sum2(&$p)
{
    return $p.='bbb';
}//既然是返回引用,自然不能使用形参传递,所以将$p改为&$p

 echo sum($a);//output is wapaaa
 echo "<br>";
 echo sum2($b);//output is kkkbbb
 echo "<br>";
 echo sum($a);//output is wapaaa
 echo "<br>";
 echo sum2($b);//output is kkkbbbbbb
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template