Forum Moderators: coopster
Probably an easy question, but darned if I can make it work!
I want to make a random number (1-10) added to another random number (1-10), the user will use a form to fill out the answer.
Output would look something like this:
6 + 8 = [_______] <--- that "box" is "text input". The six and 8 would be variables.
When I write the code for it, I can't insert a variable in front of the <input type="text"....>
Here is a little of the code:
<?php
$r1 = rand(1,10);
$r2 = rand(1,10);
$answer=($r1=$r2);
?>
<form action="1random.php" method="POST">
<input type="submit" value="Send Answer" />
</form>
It seems so simple, but I'm scratching my head...
Thanks,
KP
<?php
$r1 = rand(1,10);
$r2 = rand(1,10);
$answer=($r1+$r2);
?>
<form action="1random.php" method="POST">
<?php
echo $r1." + ". $r2;
?>
<input type="text" name="answer" />
<input type="submit" value="Send Answer" />
</form>
I didn't realize you could split the <form> lines apart. Excellent!
I have it almost... It looks like I should be setting variables to 0 before I accept input?
Not sure why, but it always prints "Sorry"....?
<?php
echo $r1." + ". $r2;
?>
<input type="text" name="answer" />
<input type="submit" value="Send Answer" />
</form>
<?php
echo $answer;
$a = $_REQUEST['answer'];
echo $a;
if ($answer=$a)
{
echo "Great!";
}
echo "Sorry";
if ($answer=$a)
This is not a test of equality yet though, just so you know! Use the double equal sign. You'll want to read through assignment [php.net] and comparison [php.net] to understand the difference.
I was working on a small routine earlier & was using"
if ($a=$b)
do my routine
else
quit
I think $a & $b were strings.
Anyhow, they were always equal. I finally figured it must have been assigning $b value into $a. Which, i think, always made the statement true. That took a while to figure.
So what's the difference between == and ===. I believe I used === on the routine today (and it worked).
Thanks!
kp