给出以下示例:
public function replaceMyText($search, $replace, &$content)
{
$newContent = str_replace($search, $replace, $content, $count = 1)
$content = $newContent;
}
这会导致Warning
只能通过引用传递变量吗?如果是这样,我无法完全理解为什么。
在将$content
传递给str_replace
函数之前,我应该将<?php
function replaceMyText($search, $replace, &$content)
{
$newContent = str_replace($search, $replace, $content, $count = 1);
$content = $newContent;
}
replaceMyText("123", "456", "123456");
分配给另一个变量吗?
Fatal error: Only variables can be passed by reference in /usercode/file.php on line 8
使用没有变量的这个函数会得到一个致命的错误
http://php.net/manual/en/language.references.pass.php
因为
不应该通过引用传递其他表达式,因为结果是未定义的。
来自$a = "123456";
replaceMyText("123", "456", $a);
echo $a;
你可以用这个来
function replaceMyText($search, $replace, $content)
{
$newContent = str_replace($search, $replace, $content,$count=1);
return $newContent;
}
$searchValue = "test";
$replaceWith = "magic trick";
$actualContent = "This is a test.";
$replaced = replaceMyText($searchValue,$replaceWith,$actualContent);
echo $replaced;
对不起,我的英语不好。我希望它可以帮到你。
编辑
请尝试以下代码:
qazxswpoi