Jump to content

Completely new to this but trying to make a dice script


captainphpnoob

Recommended Posts

Hello. My name is Sam and I'm learning php as a hobby so I understand if more urgent matters are taken care of before mines! :)
 
My goal for now is to create a dice script where I roll a die and it prints out how many times it had to roll before it lands on a 6.
 
Here is the code I have thus far.

 

 

 

<?php
        //write your do-while loop below
        $do {$roll = rand(1,6);
            $rollCount ++;
           switch ($roll) {
               case(1):
               case(2):
               case(3):
               case(4):
               case(5):
                break;
                case(6):
                    echo "<p>It took {$rollCount} times!</p>";
                    break;
           }
                           
          }
 
    ?>
 
 
but nothing happens. Sorry for the trouble. I find php fun so far and would love to learn more. Thanks for any help!

If you want a pictorial record of the throws you could

 

$count=0;
do {
    $roll = rand(1,6);
    $count++;
    echo "<img src='dice.php?n=$roll' />";    // dice.php attached
} while ($roll != 6);
echo ' ' . $count . ' rolls<br>';

 

dice.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.