简体   繁体   English

在函数PHP中无法从str_replace获得回显

[英]Can't get echo from str_replace in function, PHP

class StringsAndStuff {

    public $repeatedString = "no means no";

    public function stringRepeat($startString, $repeatCount) {
        $this->repeatedString = str_repeat ($startString."</br>", $repeatCount);
    }


    public function stringSwitchWords($multipliedString) {
        $this->repeatedString = str_replace("no", "yes", $multipliedString);        
    }

}

$stuff = new StringsAndStuff;
$stuff->stringRepeat($stuff->repeatedString, 5);
echo $stuff->repeatedString;

//why this won't work?
$stuff->stringSwitchWords(repeatedString);
echo $stuff->repeatedString;

I want to get a "yes means yes" string from echo at the end, but it won't work. 我希望最后从echo得到一个“是的意思是”字符串,但它不起作用。 Please, help me. 请帮我。 Thank you for all your answers. 谢谢你的所有答案。

EDIT: Output: 编辑:输出:

no means no
no means no
no means no
no means no
no means no
repeatedString

I think it should be something more like "yes means yes" repeated 5 times. 我认为这应该是更像是“是的意味着是”重复5次。

Your original code: 你原来的代码:

$stuff->stringSwitchWords(repeatedString); // repeatedString is empty
echo $stuff->repeatedString;

This should work: 这应该工作:

echo $stuff->stringSwitchWords($stuff->repeatedString);

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM