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

时间:2014-02-02 13:55:47

标签: php echo str-replace

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;

我希望最后从echo得到一个“是的意思是”字符串,但它不起作用。请帮我。谢谢你的所有答案。

编辑: 输出:

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

我认为它应该更像是“是的意味着是”重复5次。

1 个答案:

答案 0 :(得分:2)

您的原始代码:

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

这应该有效:

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

Demo