user3262888
user3262888

Reputation: 1

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. 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.

Upvotes: 0

Views: 132

Answers (1)

ddlab
ddlab

Reputation: 930

Your original code:

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

This should work:

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

Demo

Upvotes: 2

Related Questions