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

While Loops in PHP Forum

View Course » View Exercise

598 points
Submitted by
Hylotl Galactic Dominion
about 4 years ago

I am confused by why/how this code works

The code for this lesson is:

<!DOCTYPE html>

Coin Flips

We are going to flip a coin until we get three heads in a row!
$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
$headCount ++;
echo "H";
else {
$headCount = 0;
echo "T";
echo "It took {$flipCount} flips!";

I get most of it. The only thing that stumps me is if/else statement. If ($flip) is set = to rand(0,1), then EVERY case will be ($flip) because you'll always have a random value between 0 and 1. As such, it would seem that with this line of code, every flip would be heads. So how do you get a tails flip then? It doesn't make sense to me.


1 vote


I think the rand function isn't actually randomizing between the numbers 0 and 1. The numbers 0 and 1 in the rand function could possibly not represent values but instead represent states of true and false.

I understand your plight with this, because having knowledge of the fact that a 0 and 1 could also represent states of true and false requires some low-level knowledge of computers and the rand function situation isn't explained either.

Your best bet would be to go see how the rand function works in the PHP manual on their site.

690 points
Submitted by
about 4 years ago


907a245be96baeb3923ef88e454b6bc2?s=140&d=retro Mike about 4 years ago

My problem is even more basic, I am stuck on a concept! Having set $flip to a random number in the range of 0 - 1, what is $flip then being tested for?

$flip = rand(0,1);
$flipCount ++;
if ($flip){

It cannot be "true" or "false" as we have set it to be 1 or 2, so why does the test work?

Picture cseto almost 4 years ago

Looks like it's not a range. rand returns either 0 or 1; 0 being false state and 1 being true state. The 0 and 1 business stems from the binary language.

52c3516b80ff335abc0034f6_717051664 Alex Jean-Baptiste over 3 years ago

The basic thing not being discussed in this lesson is the boolean data type. Boolean is a function common to programming languages where 0 and 1 can be used as false and true respectively. What also isn't clear here (and what I couldn't find on the internet) is what the symbol "++" indicates after $headCount and $flipCount in the code. From the function of the program and the structure of the code I want to assume it simply just adds to the already defined variable values. Am I correct?

D40259d3c97b5ce551d23c6e409eb7e9?s=140&d=retro hain54 over 3 years ago

@Alex, '++' is an increment tool. It is the equivalent of '+1'.

4faecd7f04c1a10003014355_946019411 Igor Vilinchuk over 3 years ago

I also was stuck with this question and cleared it up here:

0 votes


simple, rand(0,1) returns either 0 or 1. 0 is interpreted as false, and 1 is a true value. it's all about boolean values! Then the if statement is written in a simple though confusing way (sometimes for beginning learners), the if($flip) is essentially saying if($flip === true) do the following . Assuming you know the difference between comparison and assignment operators.

383 points
Submitted by
Joda Cokol
over 3 years ago