FAQ: Loops - do while

This community-built FAQ covers the “do while” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise do while

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

<?php $plant_height = 22; do{echo "The plant is $plant_height tall.\n"; $plant_height++; if($plant_height >= 30){echo "And can produce fruit.";}else{}}while($plant_height<=30); I did this as the instruction says ' When the plant is greater than or equal to 30, you should print `"And can produce fruit." ', however, this actually outputs 'The plant is 22 tall. ...... The plant is 29 tall. And can produce fruit.The plant is 30 tall. And can produce fruit.' instead when I change the if condition to '$plant_height >30', the output becomes ' ... The plant is 29 tall. The plant is 30 tall. And can produce fruit.' just like the exercise expects . is there any error in my code ?

when you want to paste your code for us to read it, you should use the button </> on top of the text box and then paste your code where it says so. That way, it’s gonna be easier for us to read it and help you out.

the result will look like this

<?php
  $plant_height = 22;

do {
  echo "The plant is $plant_height tall.\n";
    if ($plant_height >= 30) {
    echo "And can produce fruit.\n";
    }
  $plant_height++;
} while ($plant_height <= 30);

your problem is that you didn’t put the if code at the right place, i figured it out by trying to put it in the beginning of the do loop, then after the echo and finally after the $plant_height++ . Coding is all about trying, making mistakes, learning from them and searching the net :wink:

1 Like

Thanks for clarifying the positioning of the if statement for this!

I didn’t read the instructions very carefully so omitted the if, and used:

<?php
  $plant_height = 50;
do {
    echo "The plant is $plant_height tall.\n";
    $plant_height += 1;
  } while ($plant_height <= 30); {
    echo "And can produce fruit.";
  }

Echoing my $plant_height after this block returns 31, but this answer still passed the lesson, so I couldn’t check the solution to see where I was going wrong

1 Like

I am having a weird issue. I managed to get to the solution alright but there is an error I don’t understand and I was hoping someone could help me shed some light in the matter.

This works great and outputs what is expected

<?php $plant_height = 22; do { echo "The plant is " . $plant_height . " tall.\n"; if($plant_height >= 30){ echo "And can produce fruit.\n"; } $plant_height ++; } while ($plant_height <= 30);

The plant is 22 tall.
The plant is 23 tall.
The plant is 24 tall.
The plant is 25 tall.
The plant is 26 tall.
The plant is 27 tall.
The plant is 28 tall.
The plant is 29 tall.
The plant is 30 tall.
And can produce fruit.

But this:

<?php $plant_height = 22; do { echo "The plant is " . $plant_height . " tall.\n"; if($plant_height = 30){ echo "And can produce fruit.\n"; } $plant_height ++; } while ($plant_height <= 30);

Only outputs

The plant is 22 tall.
And can produce fruit.

I don’t understand why :S! Should the code not go through the code inside the loop the same?

Nevermind. I figured it out :blush:
I used only one = instead of using double. Silly me.