rollDice3.php

Source of rollDice3.php

<?php
  session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/xml; charset=utf-8" />
    <title>rollDice3.php</title>
  </head>

  <body>
    <h1>RollDice 3</h1>
    <h2>Uses a Session Variable</h2>
    
    <?php

function rollDie(){
  global $total;

  $roll = rand(1,6);
  $image = "die$roll.jpg";
  print <<< HERE
    <img src = "$image"
         alt = "roll: $roll" />
         
HERE;
  $total = $_SESSION["total"];
  $total += $roll;
  $_SESSION["total"] = $total;

} // end rollDie

$count = $_SESSION["count"];
$count++;
$_SESSION["count"] = $count;

rollDie();

print "    <p>Rolls: $count</p> \n";
print "    <p>Total: $total</p> \n";

    ?>
  </body>
</html>