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

  • While I'm reading through your code, can you go ahead and describe what happens instead? It won't work is something I expect from my mom diagnosing a program bug ;) – Zarathuztra Feb 02 '14 at 13:57
  • 2
    unsure if it is a copy/paste error, but "repeatedString" is not a variable here so its probably throwing an error $stuff->stringSwitchWords(repeatedString); – lonewolf217 Feb 02 '14 at 13:57
  • Do you have a constant called `repeatedString` that you're using in `$stuff->stringSwitchWords(repeatedString);`? – Mark Baker Feb 02 '14 at 13:58
  • Function stringSwitchWords() is supposed to change words "no" to "yes" in given string. – user3262888 Feb 02 '14 at 14:02
  • 1
    Maybe you're looking for this instead? $stuff->stringSwitchWords($stuff->repeatedString); – Will Feb 02 '14 at 14:05

1 Answers1

2

Your original code:

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

This should work:

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

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
ddlab
  • 918
  • 13
  • 28
  • @user3262888 You should propably adjust your errorlevel to show notices as well, then you could have caught this yourself: http://ideone.com/M0n66P – Potherca Feb 02 '14 at 14:22
  • @Potherca: It's best to edit your `php.ini` configuration file and enable the directives. And on SO, I usually suggest folks to add [these lines](http://stackoverflow.com/a/6575502/1438393) at the very top of their script to enable error reporting :) – Amal Murali Feb 02 '14 at 14:28