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

While Loops in PHP Forum

View Course » View Exercise

395 points
22b127317ff3fb2669498debfd901fe5?s=140&d=retro
Submitted by
Evgeniy
over 4 years ago

I don't understand what "if ($flip)" does

A can't catch what condition "if ($flip)" does.

In previous lessons were only f.i. if ($flip <10) - that I can get: the variable $flip should be less then 10. Ok. But what should be $flip in if($flip) when there is nothing to be compared with?!

I'm really stuck.

And BTW, I also do not understand what do the $variavle = true / false. Compared to what the're true or false?

Thanks in advance.


18 votes

permalink

True and false can also be represented with integers where 1 is true and 0 is false. If we create a variable that is assigned an integer of either 1 or 0 (as in the case of the $flip code), PHP can evaluate the variable as either "true" or "false".

In a loop, the code inside the curly brackets is executed depending on whether the calculation inside the parenthesis evaluates to true or false. When your code shows ($flip < 10), it compares the value of $flip against 10 and returns "true" if $flip is less than 10 or "false" if greater than or equal to 10 and then proceeds accordingly.

In the case of ($flip), the code checks to see if the value of $flip is 1 or 0 and then returns "true" or "false" accordingly. Make sense?

On a side note, when using a number variable as a boolean (true/false), ANY number other than 0 returns as TRUE.

827 points
C59849b88a8cb058504449eebb400e25?s=140&d=retro
Submitted by
Eric Smith
over 4 years ago

3 Comments

Picture Turancan Salur over 4 years ago

great response, thank you

55e3f23b86f552a32d0001f7_388253409 Uchenna Okoye over 4 years ago

thanks for this response. I was stuck on this, too.

4faecd7f04c1a10003014355_946019411 Igor Vilinchuk over 3 years ago

Thanks a lot for your response!