MyBB Community Forums

Full Version: PHP Number Guess
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm learning PHP, and I am trying to make a simple number script. I have made one, but it Does not detect the right answer if it is right, it tells you it's wrong.
These are the 2 files.

test.html:

<html>
<head>
<title>Test</title>
</head>
<body>
<div align="center">Please guess a number! <br/>
</div>
<form action="test2.php" method="POST" name="guess" id="guess">
<div align="center">
<p>
<input name="guess1" type="text" id="guess1" size="2" maxlength="2">
<input name="guesssub" type="submit" id="guesssub" value="Guess!">
</p>
</div>
</form>
</body>
</html>

test2.php:

<?php
$number = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" );
shuffle($number);
if ( $number == $_POST[".guess"] ) {
echo "Great Job! Your number was: $number";
} else {
echo "Please try again. The number was: $number[0]";
}
?>
any Ideas?
shuffle() returns true or false, you're comparing true or false to a number...

http://us3.php.net/manual/en/function.shuffle.php

What you want is this:

<?php
$number=rand(1,10);
if ( $number == $_POST["guess1"] ) {
echo "Great Job! Your number was: $number";
} else {
echo "Please try again. The number was: $number";
}
?>
(2009-06-17, 08:46 PM)Anman Wrote: [ -> ]shuffle() returns true or false, you're comparing true or false to a number...

http://us3.php.net/manual/en/function.shuffle.php

What you want is this:

<?php
$number=rand(1,10);
if ( $number == $_POST["guess1"] ) {
echo "Great Job! Your number was: $number";
} else {
echo "Please try again. The number was: $number";
}
?>

Ahh! This php stuff hurts my head because I don't know! I told myself I would learn it this summer, but I started to learn it and got confused.
I would suggest to every new users to PHP to code it with structure. I hate looking at coding that's this messy. Just add some spaces and whatnot. You should learn from MyBBs source, although it's old it's quite easy to understand if you understand PHP.
PHP is quite easy but you have to be creative to do anything with it, and of course it's not for everyone.
(2009-06-17, 10:47 PM)Martin M. Wrote: [ -> ]I would suggest to every new users to PHP to code it with structure. I hate looking at coding that's this messy. Just add some spaces and whatnot. You should learn from MyBBs source, although it's old it's quite easy to understand if you understand PHP.
PHP is quite easy but you have to be creative to do anything with it, and of course it's not for everyone.

I only just realised you've hard your name changed, I thought for a minute someone was copying your usertitle and sig RolleyesToungue
(2009-06-17, 10:50 PM)MattRogowski Wrote: [ -> ]
(2009-06-17, 10:47 PM)Martin M. Wrote: [ -> ]I would suggest to every new users to PHP to code it with structure. I hate looking at coding that's this messy. Just add some spaces and whatnot. You should learn from MyBBs source, although it's old it's quite easy to understand if you understand PHP.
PHP is quite easy but you have to be creative to do anything with it, and of course it's not for everyone.

I only just realised you've hard your name changed, I thought for a minute someone was copying your usertitle and sig RolleyesToungue

Yea I got it changed. I was trying to start fresh Rolleyes