【发布时间】:2016-08-01 09:28:18
【问题描述】:
我正要制作一个过滤器助手,并通过引用传递的基本替换功能来测试我的想法,但它不起作用!
<?php
$text ="hello world i am here !";
function findandreplace(&$text, $search, $replaced)
{
return str_replace($search, $replaced, $text);
}
print findandreplace($text,'e','E');
print "<br>";
print $text;
输出总是这样:
hEllo world i am hErE !
hello world i am here !
我尝试了可能的事情,但我不会工作,那是我的错。
【问题讨论】:
-
如果您使用的是按引用传递,为什么还要返回 $text?.... 但是
str_replace()本身不是按引用传递,它返回修改后的值 -
我尝试使用 return 然后我 omtt 它,它也不会工作,但没有 return ,它给出了一个通知 [注意:只有变量引用应该通过引用返回]
标签: php pass-by-reference