FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Problems with a Guess a Number Code.





Dougie1
OK. I have spent a while on this code and have not managed to get it to work. I get the following error:
Code:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Here is the part of the code that I am having problems with. Any help will be greatly appreciated.
Code:
<?PHP if($_POST['number']) {echo "<div align=\"center\">Your guess was $_POST['guess'].</div></br>";
$guess = $_POST['guesses'];
$number = $_POST['number'];
$number = substr($number, 60, -60);
if $_POST['guess'] == $number; {echo "Your Guess Is Correct!!!  Well Done!!!  It took you $guess guesses.";}
else if ( $_POST['guess'] > $number ) && ( $_POST['guesses'] < $number + 20 ) {echo "Your guess was close but too high...  You have had $guess guesses.";}
else if ( $_POST['guess'] < $number ) && ( $_POST['guesses'] > $number - 20 ) {echo "Your guess was close but too low...  You have had $guess guesses.";}
else if ( $_POST['guess'] < $number - 20 ) && ( $_POST['guess'] > 0 ){echo "Your guess was quite a bit off...  Unlucky.  Try again and see if you can get closer.  Hint: Go Higher!!!  You have had $guess guesses.";}
else if ( $_POST['guesses'] > $number + 20 ) && ( $_POST['guesses'] < 101 ){echo "Your guess was quite a bit off...  Unlucky.  Try again and see if you can get closer.  Hint: Go Lower!!!  You have had $guess guesses.";}
else {echo "You have either guessed a number out with the range (1 to 100) or input an illegal character or the script is brocken!!  Try again.  You have had $guess guesses.";}

} else {};
?>
MrBlueSky
Look at this ( partially working) code:

Code:

<?PHP
if ($_GET['number']) {
    $guess = $_GET['guess'];
    echo "<div align='center'>Your guess was $guess.</div></br>";
    $number = $_GET['number'];
    $number = substr($number, 60, -60);
    $guesses = $_GET['guesses'];

    if ($guess == $number) {
        echo "Your Guess Is Correct!!!  Well Done!!!  It took you $guesses guesses.";
    } else if (($guess > $number ) && ( $guesses < $number + 20 )) {
        echo "Your guess was close but too high...  You have had $guesses guesses.";
    } else if (( $guess < $number ) && ( $guesses > $number - 20 )) {
        echo "Your guess was close but too low...  You have had $guesses guesses.";
    } else if (( $guess < $number - 20 ) && ( $guess > 0 )) {
        echo "Your guess was quite a bit off...  Unlucky.  Try again and see if you can get closer.  Hint: Go Higher!!!  You have had $guesses guesses.";
    } else if (( $guesses > $number + 20 ) && ( $guesses < 101 )) {
        echo "Your guess was quite a bit off...  Unlucky.  Try again and see if you can get closer.  Hint: Go Lower!!!  You have had $guesses guesses.";
    } else {
        echo "You have either guessed a number out with the range (1 to 100) or input an illegal character or the script is brocken!!  Try again.  You have had $guesses guesses.";
    }

}

?>


You had several errors:
- when using arrays in strings put them between {}
- the condition in if statements must be between () :

Code:

if (1) {
  // do something
}


- you had several of this constructions:

Code:

if (x==y) && (z==w) { // do something }


which should be
Code:

if ((x==y) && (z==w)) {
  // do something
}



Some points:

Try to use consistent indentation in your code, it makes it easier to debug it. Very Happy

Instead of using $_POST['guess'] serveral times, use the variable you assigned ($guess).

Using GET instead of post makes the debugging easier.

And what is this supposed to do?

Code:
    $number = substr($number, 60, -60); 
Dougie1
Thanks for that in depth solution that was fantastic!

I see what you mean by the indenting, it makes it look neater, easier to read!

Why does GET make it easier to debug?

Code:
    $number = substr($number, 60, -60);

That is so that I can post the number but have it with random characters around it so people can't just look at the source to find out the answer! I think I can do that more efficiently with sessions or something but I though I would stick with things I know.

[EDIT] You can see the final script here. Thanks for the help! I am pleased with it Smile
MrBlueSky
Dougie1 wrote:
Thanks for that in depth solution that was fantastic!


Glad I could help Very Happy

Quote:

Why does GET make it easier to debug?


Not always, but using GET you can specify the variables in the url so you don't need a form. I personally find this often quicker and easier when debugging.

Quote:

[EDIT] You can see the final script here.


Nice!

EDIT:

Two things you can improve:
- check if the user enters a number and not something else
- reset the guesses-counter and number when the user has guessed it
Related topics
Know ur age!!!
Co myslicie o mojej stronce??
Player script
What's your Video Card?
CVS server on Windows
File permissions (Server 1)
Doctor HTML - analyse your page
Problems with a new windows install
Error connecting to mysql ; please help
The Daily Guess A Number Game: Day 111
php 4
to show "403 Forbidden" Directories
Guess numbers of candies in jar-method?
Javascript: Multiply string with number
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.