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!".

EXTENSION

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!)

EXTENSION 2

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 here.

Comments

Your email address will not be published.

PHP for Beginners

Enrol in the full course here

PHP, MySQL and PDO

Coming soon, a full course on using PHP, MySQL and PDO. Be notified as soon as the course goes live.