In the game Keno you might have noticed that games begin at 001 and iterate to 999. Let’s look at some code to cycle properly and stay formatted in three digits using sprintf().

$game_number = 1;
echo ‘Game #’ . sprintf(“%03d”,$game_number); // Game #00x
 

Now let’s add some style magic to give the output a blackground with red numbers:

$game_number = 1;
echo ‘New game #<font style="background-color: black;color: red">’ . sprintf(“%03d”,$game_number) . ‘</font><br />’;

Finally we need to create some program logic to test when to reset the game number to 1. An IF statement will suffice and we’ll also add some code to go to show the next game number.

$game_number = (int)$_GET[‘gid’];
if($game_number > 999 or $game_number < 1) {
$game_number = 1;
}
echo ‘New game #<font style="background-color: black;color: red">’ . sprintf(“%03d”,$game_number) . ‘<br />’;
$game_number++;
echo “<p><a href=\”?gid=$game_number\”>next game</a></p>”;