Hi. I'm wondering how to count the output of rand(). Here's the layout for my code so far:
$flip=rand(0,1);
if $flip ==0 then heads
if $flip == 1 then tails
I need to then use a while loop to continue the rand function until there is 1 heads and 1 tails. How would I tell php to count this?
The Output must look like this:
Person: Heads, Tails Person 1: 1, 9 Person 2: 2, 1 Person 3: 5, 1 ....
etc.,
You can use sessions and count. like this:
<?PHP session_start(); $flip = rand(0,1); //bobs flipping a coin if(isset($_POST['bob'])){ if($flip == '0'){ if (!isset($_SESSION['bobHeads'])) $_SESSION['bobHeads']=1; else $_SESSION['bobHeads']++; }elseif($flip == '1'){ if (!isset($_SESSION['bobTails'])) $_SESSION['bobTails']=1; else $_SESSION['bobTails']++; } } //carls flipping a coin if(isset($_POST['carl'])){ if($flip == '0'){ if (!isset($_SESSION['carlHeads'])) $_SESSION['carlHeads']=1; else $_SESSION['carlHeads']++; }elseif($flip == '1'){ if (!isset($_SESSION['carlTails'])) $_SESSION['carlTails']=1; else $_SESSION['carlTails']++; } } ?> <html> <body> bob Heads: <?php echo ($_SESSION['bobHeads']); ?> <br/> bob Tails: <?PHP echo ($_SESSION['bobTails']); ?> <form method="post" action="<?PHP $_SERVER['PHP_SELF']; ?>" > <input type="submit" name="bob" value="bob"> </form> <br/> carl Heads: <?php echo ($_SESSION['carlHeads']); ?> <br/> carl Tails: <?PHP echo ($_SESSION['carlTails']); ?> <form method="post" action="<?PHP $_SERVER['PHP_SELF']; ?>" > <input type="submit" name="carl" value="carl"> </form> </body> </html>
hope that helps. j
Thank you very much. I'll definitely look at your example as an alternative method, and edit my code accordingly. I managed to solve it though. =] Thanks again for your quick response.
To be honest, based on what you wrote in your original post, I would not suggest sessions, since you stated nothing about persisting the state between page loads. All you need is two variables: one to store the count of heads, another to store the count of tails. An array could also suffice.