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

While Loops in PHP Forum

View Course » View Exercise

502 points
9badb22d172ada0858719177f51ad2e7?s=140&d=retro
Submitted by
STEVEN FONTANA
over 2 years ago

Condition

I have seen the following code on this forum:

while ($loopCond)

This doesn't make sense to me. Doesn't there need to be a comparison or boolean inside the parentheses? Also, do we need curly braces after this line?


2 votes

permalink

In this example, $loopCond is a boolean variable, which means it's legal to use the shorthand notation "while($loopCond)", which is just an abbreviated but equivalent way of saying "while($loopCond==true)". You can do the same in if statements, e.g. if($heads), which is equivalent to if($heads==true).

202 points
C3c92b0201bec38db96ae247d147f08f?s=140&d=retro
Submitted by
rdols
over 2 years ago


0 votes

permalink

$i = 5;

while($i < 5){
echo "infinite loop!;
}

You have a variable declared outside the loop, but use it inside the condition :)

450 points
5240e713abf821aa28001257_703076796
Submitted by
Robinvb
over 2 years ago