Falco1199
09-16-2002, 05:04 PM
I just made a random practice script to test my knowledge of PHP after learning it. It comes up with an error... Here's the script:
<html>
<head>
<title>PHP Test!</title>
</head>
<body>
Try to guess my number! Good luck.<br /><br />
<form action="<?php $PHP_SELF ?>">
Print a number between 1 and 6:<input type="text" length="10" name="number"><br />
<input type="submit" value="Let's see!"><br />
</form>
<?php
if (IsSet($number) {
if (!is_integer($number) or $number < 1 or $number > 6)
print("Your input was not a number from one to six.");
else
{
mt_srand((double) microtime() * 1000000);
$num = mt_rand(1,6);
if ($num == $number)
print("A WINNER! <a href="winner.php">Click here to claim your prize!</a>);
else
print("Sorry, that's not that number");
}
}
?>
</body>
</html>
And here's my error:
Parse error: parse error in /home/virtual/site141/fst/var/www/html/phptest.php on line 13
What's wrong?
bowhuntr
09-16-2002, 05:12 PM
I'm just learning this stuff, but it looks like you are missing some " on line 21
Falco1199
09-16-2002, 05:31 PM
if ($num == $number)
quotes??
bowhuntr
09-16-2002, 05:44 PM
yes, look at the quotes in the print statement for the winner line (21)
shouldn't it be:
print("A WINNER! <a href="winner.php">Click here to claim your prize!</a>" );
Considering matching brackets as a first step.
Lats...
tapster
09-16-2002, 06:21 PM
if (IsSet($number) {
has the closing bracket missing after $number as Lats pointed out...
you also want to escape the " inside your href tag or use single quote around the filename
:)
Earthnet
09-16-2002, 07:08 PM
Fix:
print("A WINNER! <a href="winner.php">Click here to claim your prize!</a> );
to
print("A WINNER! <a href=\"winner.php\">Click here to claim your prize!</a>" );
if (IsSet($number) {
to
if (IsSet($number)) {
To make the script work correctly (after all the parsing errors are fixed) use is_numeric instead of is_integer.
:)
jtrovato
09-19-2002, 02:41 AM
I don't know about you guys but the
if (something) {
}
Statement kills me
20 years ago when I learned C on an Apple IIe and from then on I have always used the following structure
If (c1ondition)
{
if (Some thing else is true)
{
Do some more things
}
}
else
{
do something else
}
I had to write huge apps and I’ll tell you one thing, to debug would have been crazy if I didn’t see what statements went with what code.
Some food for thought
John
jtrovato
09-19-2002, 02:43 AM
well it looks like my tabs didn't work. well this is what I ment sorry guys
If (c1ondition)
{
if (Some thing else is true)
{
Do some more things
}
}
else
{
do something else
}
jtrovato
09-19-2002, 02:45 AM
you get the idea. no spaces or tabs allowed here. lol Maybe I should learn HTML!!!!!
If (c1ondition)
{
-----> if (Some thing else is true)
----->{
----->----->Do some more things
----->}
}
else
{
----->do something else
}
michaeln
09-19-2002, 08:14 AM
Ok there are a coupld of errors in your script. Here is a working version:
<html>
<head>
<title>PHP Test!</title>
</head>
<body>
Try to guess my number! Good luck.<br /><br />
<form action="<?php $PHP_SELF ?>" method="post">
Print a number between 1 and 6:<input type="text" maxlength="1" length="10" name="number"><br />
<input type="submit" value="Let's see!"><br />
</form>
<?php
if (IsSet($number))
{
if ($number < 1 || $number > 6)
print("Your input was not a number from one to six. <BR>You input $number");
else
{
mt_srand((double) microtime() * 1000000);
$num = mt_rand(1,6);
if ($num == $number)
print("A WINNER! <a href=\"winner.php\">Click here to claim your prize!</a>");
else
print("Sorry, that's not that number");
}
}
?>
</body>
</html>
!is_integer($number) had to be removed as form input is passed as a string and therefor will always return false.
You forgot a quote at the end of this line:
print("A WINNER! <a href="winner.php">Click here to claim your prize!</a> );
You had also put quotes in the middle of the above string without backslashes in front. It should look like this:
print("A WINNER! <a href=\"winner.php\">Click here to claim your prize!</a>");
Regards,
Michael
Ultravox
09-19-2002, 11:24 AM
if (IsSet($number) {
this is your line 13
correct it to
if (IsSet($number)) {
It will solve your purpose.