-1

I've been learning PHP for over a year now and it's always the little things that seem to go disastrously wrong, or I just forget what I'm doing.

I've been using Do-While loops in a few projects but recently one I created just didn't work. Once I numbed it down to almost nothing I noticed it just isn't stopping according to user input.

do {
    echo "Hi there\n";
    echo "Echo\n";
    $userInput = readline();
} while ($userInput = 'continue');
echo "Exit";

I don't understand what's going wrong but something is. From my understanding the program will echo twice, listen for the user's input and loop through again while the user types continue - if not then will echo Exit. What am I doing wrong? This is such a simple task and it's annoying me. All the other topics I've searched for don't seem to be helping.

  • 1
    `$userInput = 'continue'` is an **assignment** and will always be true. Voting to close because the problem is caused by a typo. – Quentin Jan 24 '19 at 09:44
  • What @Quentin said – Nikola Gavric Jan 24 '19 at 09:44
  • `==` is what you are searching for –  Jan 24 '19 at 09:44
  • This is just an example of me being tired beyond belief. I knew this was something so simple and although embarrassing, I'll keep this up for anyone in the future. Thanks Quentin, Nikola and Maurice. – Nathan Harrison Jan 24 '19 at 09:44
  • Try use $userInput == 'continue', not $userInput = 'continue' – Vitalii Jan 24 '19 at 09:45
  • @NathanHarrison — Please don't keep it up for people in the future. Nobody with this problem will ever manage to find a search term that stumbles across this question. Better to delete it to remove cruft from SO. – Quentin Jan 24 '19 at 09:48

1 Answers1

-1

You need to use

while ($userInput == 'continue');

instead of

while ($userInput = 'continue');

because you have used wrong operator (= [assign] instead of == [equals])

puffy.bun
  • 256
  • 1
  • 10
  • 1
    Please don't answer questions where the solution is "Fix the typo". This type of question has no long term value since other people with the same problem are not going to find this by searching. "Typo" is one of the standard Close Vote reasons. Answering this type of question can block the OP from deleting it to remove bloat from StackOverflow. – Quentin Jan 24 '19 at 09:47