This forum is now read-only. Please use our new forums at discuss.codecademy.com.

While Loops in PHP Forum

View Course » View Exercise

509 points
Picture
Submitted by
Guilherme
over 4 years ago

While Loop Syntax

How can I answer this exercise? I`m not understanding how is the while loop syntax! Could somebody help me?


1 vote

permalink

Is there a specific part of the syntax or the exercise that you are having trouble with?

990 points
Picture
Submitted by
Devin West
about 4 years ago

1 Comment

Picture Guilherme about 4 years ago

Im with difficult to understand the syntax of the "While"(Im trying to solve the exercise 1.2)


1 vote

permalink

For exercise 1.2 you should not use the do/while loop syntax and instead you should use the while loop syntax

while(cond) {
   // looped statements go here
}

where the statements are executed as long as cond is true. Inside the loop body you should echo the string "<p>The loop is running.</p>".

990 points
Picture
Submitted by
Devin West
about 4 years ago


0 votes

permalink

It would be helpful if you could post your code up! Then I can try see what's wrong and add comments about what each line means :D

630 points
61f42e45177a08c6421f484026d7da84?s=140&d=retro
Submitted by
James
about 4 years ago


0 votes

permalink

<!DOCTYPE html>

Your First PHP while loop!


<?php
$loopCond = false
do {
echo "The loop ran even though the condition is false." ;
} while ($loopCond)
echo "Now the loop is running.";
?>

509 points
Picture
Submitted by
Guilherme
about 4 years ago

1 Comment

Picture Devin West about 4 years ago

The loop is not running outside of the curly braces { and } and there should also be a semicolon ; after while () statement in a do/while loop. But, the for this exercise you are not supposed to use a do/while loop. Please refer to my answer for more details.