Page 1 of 1

Homework help please

Posted: Thu Oct 16, 2008 10:32 pm
by hmmdjkawinkydink
Hello,
I'm having trouble with one of my exercises from my book and I was hoping something could help me out please. I'm not looking for you to do it for me but I'm really confused and it's an online class. The teacher isn't that much help. The Exercise is: "Write a program that lets the user choose how many sides a die will have and print out a random roll with the appropriate maximum values (don't worry about using images to display the dice)." I read the chapter and understand the examples that they gave but they don't seem thorough enough to do the exercise. My problem is that the form shows up but the "HERE;" and all the php below it also shows up. I would appreciate your help. I really want to get through this class and finally learn php.

Thank you,

Holly

Code: Select all

<html>
<head>
<title>Die Sides</title>
</head>
 
<body>
 
<?php
$die = 0;
$integer = 0;
extract($_REQUEST);
print <<<HERE
    <form>
    <h4> Pick which die to roll and press ROLL: </h4>
    <input name="die" type="radio" value="100" />6 sides<br>
    <input name="die" type="radio" value="200" /> 9 sides<br>
    <input name="die" type="radio" value="400" /> 12 sides<br>
    <input name="die" type="radio" value="1776" /> 14 sides<br>
    <input name="die" type="radio" value="45" /> 24 sides<br>
    <input value = "ROLL" type = "submit">
    </form>
    
    HERE;
    
    if ($die > 0) {
    print "You got $die";
    }
    
    switch ($die){
        case 6:
            $integer = rand(1,6);
                break;
        case 9:
            $integer = rand(1,9);
                break;
        case 12:
            $integer = rand(1,12);
                break;
        case 14:
            $integer = rand(1,14);
                break;
                
        case 24:
            $integer = rand(1, 24);
                break;
                
    }
    
if (!empty($integer)) {
    print "<p>You rolled $integer !</p>";
    $die = 0;
    }
    
    ?>
    
    
    </body>
    </html>

Re: Homework help please

Posted: Thu Oct 16, 2008 11:19 pm
by requinix
Quite simple, actually. When using heredoc the identifier (thing after the <<<) has to be the absolute first thing on the line. That means no indentation either.

Code: Select all

print <<<HERE
    <form>
    <h4> Pick which die to roll and press ROLL: </h4>
    <input name="die" type="radio" value="100" />6 sides<br>
    <input name="die" type="radio" value="200" /> 9 sides<br>
    <input name="die" type="radio" value="400" /> 12 sides<br>
    <input name="die" type="radio" value="1776" /> 14 sides<br>
    <input name="die" type="radio" value="45" /> 24 sides<br>
    <input value = "ROLL" type = "submit">
    </form>
   
HERE;
It has to be the only thing on the line as well - a semicolon is the only exception.

If you're dedicated to learning PHP you might be interested in hearing more about that code you have, like why using extract() is bad and how you can make your stuff simpler...

Re: Homework help please

Posted: Thu Oct 16, 2008 11:54 pm
by hmmdjkawinkydink
Hi,
Thank you for replying.
I tried to make the changes below but I'm getting the same results. I would be VERY interested in finding a simpler way of doing this.

Code: Select all

 
<html>
<head>
<title>Die Sides</title>
</head>
 
<body>
 
<?php
$die = 0;
$integer = 0;
extract($_REQUEST);
print 
<<<HERE
    <form>
    <h4> Pick which die to roll and press ROLL: </h4>
    <input name="die" type="radio" value="6" />6 sides<br>
    <input name="die" type="radio" value="9" /> 9 sides<br>
    <input name="die" type="radio" value="12" /> 12 sides<br>
    <input name="die" type="radio" value="14" /> 14 sides<br>
    <input name="die" type="radio" value="24" /> 24 sides<br>
    <input value = "ROLL" type = "submit">
    </form>
HERE;
if ($die > 0) {
    print "You got $die";
    }
    
    switch ($die){
        case 6:
            $integer = rand(1,6);
                break;
        case 9:
            $integer = rand(1,9);
                break;
        case 12:
            $integer = rand(1,12);
                break;
        case 14:
            $integer = rand(1,14);
                break;
                
        case 24:
            $integer = rand(1, 24);
                break;
                
    }
    
if (!empty($integer)) {
    print "<p>You rolled $integer !</p>";
    $die = 0;
    }
    
    ?>
    
    
    </body>
    </html>
 

Re: Homework help please

Posted: Fri Oct 17, 2008 12:17 am
by requinix
Not sure what problem it could be. I tried running your code exactly as it is and it works for me. Maybe I was imagining it, but did you change the values of the different radio buttons? Did you remember to ctrl+refresh the page?

Notice how the code for each case in the switch is the same? The one difference is that the maximum number for rand() changes, and it just so happens that it's the same value as $die... So why not replace the 6, 9, 12, 14, and 24 with $die:

Code: Select all

   switch ($die){
        case 6:
            $integer = rand(1,$die);
                break;
        case 9:
            $integer = rand(1,$die);
                break;
        case 12:
            $integer = rand(1,$die);
                break;
        case 14:
            $integer = rand(1,$die);
                break;
               
        case 24:
            $integer = rand(1, $die);
                break;
               
    }
But now all the different cases are the same, so do you even need a switch in the first place?

Re: Homework help please

Posted: Fri Oct 17, 2008 5:45 pm
by califdon
tasairis wrote:using extract() is bad...
I'm interested in knowing that, too. I use extract() all the time.

And thanks, tasairis, for your many helpful posts, including this one.

Re: Homework help please

Posted: Fri Oct 17, 2008 6:24 pm
by requinix
califdon wrote:
tasairis wrote:using extract() is bad...
I'm interested in knowing that, too. I use extract() all the time.

And thanks, tasairis, for your many helpful posts, including this one.
Well, I was exaggerating a bit. It has its good uses just like eval() and register_globals, but it has its bad uses too.

OP used it on $_REQUEST, so everything in there gets extracted into a variable. Like GET stuff. Emulating register_globals, really.
Let's say I went to page.php?die=%3Cscript%3Ealert%28%22XSS%22%29%3B%3C%2Fscript%3E. $die>0 is true so it echos $die. Which is an XSS thing.
Same if $die isn't one of those values and the XSS is in $integer.

Re: Homework help please

Posted: Fri Oct 17, 2008 8:55 pm
by hmmdjkawinkydink
Thank you :-). It's still not working for me. I'm not sure what's going on. It's weird that it works for you and not for me. I'm going over the examples in my book again to look for another way to do this that I understand better.

Holly