If/else being broken by form tags

I picked up PHP 5/MySQL Programming for the Absolute Beginner from the library, and have been working through the examples in the book to learn the language. Two days ago, I hit a brick wall. I’ve tried everything I can think of to debug it, and can’t figure it out. I’m pretty sure part of my problem is the book’s a bit outdated, as the form example in the book didn’t work either. I’d really appreciate it if someone would take a look at my code and tell me where the problem is.

<h1>generates 4, 10, or 20 sided dice</h1>

if (empty($diceside)){
	//switch to HTML mode to display form
	<form action = "diceroll.php" method = "post">
	Which dice do you want to roll?<br><br>
	<input type = "radio" name = "diceside" value = "4"> 4 <br>
	<input type = "radio" name = "diceside" value = "10"> 10 <br>
	<input type = "radio" name = "diceside" value = "20"> 20 <br>
	<input type = "submit"> <br>

} else if (diceside==4) {
	$roll = $rand(1,4);
} else if (diceside==10) {
	$roll = $rand(1,10);
} else if {diceside==20) {
	$roll = $rand(1,20);
} else {
	print ("I don't know that number.");
} //end if

if (!empty($diceside)){
	print ("You rolled a $roll!");


A book discussing PHP5 should be teaching the register_globals mechanism. Your $diceside value isn’t picked up by PHP, because this mechanism is disabled by default (and for good reason). Use $_POST[‘diceside’] instead.

Sponsor our Newsletter | Privacy Policy | Terms of Service