THAPA TECHNICAL

HOUSE OF WEB DEVELOPERS AND TECHNOLOGY.

PHP challenge HEADS, I WIN! TAILS YOU WIN!

HEADS, I WIN! 

This challenge tests your understanding of the RAND() function and 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   

This extension challenge tests your understanding of:   
● RAND()
● IF­ELSE
● FOR loop
● concatenation   

Use a loop to generate the above 100 times. 

Keep a score of how many heads there were and how many tails and then echo out the results as

There were 48 heads and 52 tails.  

5 comments:

  1. Sir main page of an ecommerce website ka kaise banae with php

    ReplyDelete
  2. $heads = 1;
    $teals = 1;
    for ($i = 1; $i <= 100; $i++) {
    // echo "$i

    ";
    $randomNum = rand(1, 2);
    if ($randomNum == 1) {
    echo "$randomNum Heads I win

    ";
    $heads++;
    } else {
    echo "$randomNum Teals you win

    ";
    $teals++;
    }
    }
    echo "Total Number of Heads $heads

    ";
    echo "Total Number of teals $teals

    ";
    if ($heads > $teals) {
    echo "Heads, I win!

    ";
    } else {
    echo "teals, you win!

    ";
    }

    ReplyDelete