PHP Challenge 6 - Heads I Win, Tails You Lose

This challenge tests your understanding of generating random numbers and testing using the IF-ELSE statement.

You need to generate a random number between 1 and 2.

If the random number is 1 then echo "Heads, I win!".

If the random number is 2 then echo "Tails, you win!".


Use a loop to generate the above 100 times.

Keep score of how many heads and tails there were and then echo out the result as something like this:

There were 48 heads and 52 tails.

(obviously the numbers will be generated by the script!)


Can you think of a way of using an array with the contents as:

$options = ["heads", "tails"];

and using array functions to produce a similar result?

Want to Learn More?

Become a member and get access to the member only area of this site.



Your email address will not be published.

PHP for Beginners

Would you like lifetime access to the hugely successful PHP for Beginners course? Click here to find out how.

FREE Code Snippets

FREE hints and tips delivered direct to you once per month.