Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过引用传递赢得';我不能用php工作_Php_Pass By Reference - Fatal编程技术网

通过引用传递赢得';我不能用php工作

通过引用传递赢得';我不能用php工作,php,pass-by-reference,Php,Pass By Reference,我正要做一个过滤器助手,只是通过使用引用传递的基本替换函数来测试我的想法,但它不起作用 <?php $text ="hello world i am here !"; function findandreplace(&$text, $search, $replaced) { return str_replace($search, $replaced, $text); } print findandreplace($text,'e','E'); print "&

我正要做一个过滤器助手,只是通过使用引用传递的基本替换函数来测试我的想法,但它不起作用

<?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;

我尝试了很多方法,但都不起作用,这是我的错。

您没有对传递的
$text
字符串进行任何更改,因为它不会修改传递的字符串-它接收传递的值的副本,并返回结果。信息技术 如果将
str_replace
的结果分配给
$text
变量,它将按预期工作:

 $text ="hello world i am here !";

 function findandreplace(&$text, $search, $replaced)
 {

    $text =  str_replace($search, $replaced, $text); //<-- now it will work
    return $text;

 }

print findandreplace($text,'e','E');
print "<br>";
print $text;
$text=“你好,世界,我在这里!”;
函数findandreplace(&$text,$search,$replaced)
{

$text=str_replace($search,$replaced,$text);//您不会对传递的
$text
字符串进行任何更改,因为不会修改传递的字符串-它会接收传递的值的副本,并返回结果 如果将
str_replace
的结果分配给
$text
变量,它将按预期工作:

 $text ="hello world i am here !";

 function findandreplace(&$text, $search, $replaced)
 {

    $text =  str_replace($search, $replaced, $text); //<-- now it will work
    return $text;

 }

print findandreplace($text,'e','E');
print "<br>";
print $text;
$text=“你好,世界,我在这里!”;
函数findandreplace(&$text,$search,$replaced)
{

$text=str_replace($search,$replaced,$text);//如果使用按引用传递,为什么还要麻烦返回$text?……但是
str_replace()
本身不是按引用传递的,它返回修改后的值我尝试使用return,然后对其进行了omtt,它也不起作用,但是如果没有返回,它会发出通知[注意:只有变量引用应该通过引用返回]如果使用pass-by-reference,为什么还要麻烦返回$text?…但是
str\u replace()
本身不是pass-by-reference,它返回修改后的值我尝试使用return,然后我对其进行了omtt,它也不起作用,但是如果没有返回,它会发出通知[注意:只能通过引用返回变量引用]为什么要返回通过引用传递的东西?我知道它来自OP的代码,但仍然不是多余的?@HankyPanky是的,它是redundent,我希望OP添加了返回,以便他可以运行“测试”——将输出与初始引用的变量进行比较。然后可以很容易地更改为先打印
$text
,然后调用fun然后至少在这个答案中再次打印
$text
,因为它通过OP@HankyPanky同样正确的是,也许我应该做一个编辑,指出对于任何未来的访问者,Hanks@Steve会让我大吃一惊,我添加了return,因为我在删除它时注意到了错误[注意:只能通过引用返回变量引用]为什么要返回通过引用传递的东西?我知道它来自OP的代码,但仍然不是多余的?@HankyPanky是的,它是redundent,我希望OP添加了返回,以便他可以运行“测试”——将输出与初始引用的变量进行比较。然后可以很容易地更改为先打印
$text
,然后调用fun然后至少在这个答案中再次打印
$text
,因为它通过OP@HankyPanky同样正确的是,也许我应该做一个编辑,指出对于任何未来的访问者,Hanks@Steve会让我大吃一惊,我添加了return,因为我在删除它时注意到了错误[注意:只能通过引用返回变量引用]