captainphpnoob Posted April 8, 2013 Share Posted April 8, 2013 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! Link to comment https://forums.phpfreaks.com/topic/276668-completely-new-to-this-but-trying-to-make-a-dice-script/ Share on other sites More sharing options...
Barand Posted April 8, 2013 Share Posted April 8, 2013 try $count=0; do { $roll = rand(1,6); $count++; } while ($roll != 6); echo $count; Link to comment https://forums.phpfreaks.com/topic/276668-completely-new-to-this-but-trying-to-make-a-dice-script/#findComment-1423471 Share on other sites More sharing options...
Barand Posted April 8, 2013 Share Posted April 8, 2013 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 Link to comment https://forums.phpfreaks.com/topic/276668-completely-new-to-this-but-trying-to-make-a-dice-script/#findComment-1423546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.