Jump to content
xisto Community
Sign in to follow this  
kiro

Php Guessing Script Script game..

Recommended Posts

Here's the guessing script, they guess a number and checks if they are right or wrong..


<form method="guess.php?action=yes"><input type="text" name="t_guess"><br><input type="submit" value="Guess"><?phpif($action==yes){//have the number that they are suppose to guess...$guess=14;//endif($t_guess==$guess){print "Good job! Guess was right!";}else{print "Guess was wrong! Try again!";}}?>


Thats it :)

Share this post


Link to post
Share on other sites

Here's the guessing script, they guess a number and checks if they are right or wrong..

<form method="guess.php?action=yes"><input type="text" name="t_guess"><br><input type="submit" value="Guess"><?phpif($action==yes){//have the number that they are suppose to guess...$guess=14;//endif($t_guess==$guess){print "Good job! Guess was right!";}else{print "Guess was wrong! Try again!";}}?>
Thats it  :)

66435[/snapback]

Well, that is a nice little script. I can see this being useful for people new to PHP and the like. Although this is a very simple script and it is not intended to be too complicated for beginners, I will go ahead and say that this could be a valuable resource to have, just for reference.

Share this post


Link to post
Share on other sites

If you really want to guess a number, you should use php's rand() function. Look at http://de1.php.net/rand for more information. For example, if you want someone to guess a random number between 1 and 20, replace $guess=14; with $guess=rand(1,20);. The new script would be:

<form action="guess.php?action=yes" method="post"><input type="text" name="t_guess"><br><input type="submit" value="Guess"><?phpif($action == "yes"){//have the number that they are suppose to guess...$guess=rand(1,20);//endif($_POST['t_guess'] == $guess){print "Good job! Guess was right!";}else{print "Guess was wrong! Try again!";}}?>
Note that I have made some replacements.
method="guess.php?action=yes"with action="guess.php?action=yes" method="post"
because method can only be post and action, and the target has to be set with action. I also replaced
if($action == yes){with if($action == "yes"){
because your code would return an error without single our double quotes. I also replaced
if($t_guess == $guess){with if($_POST['t_guess'] == $guess){
because now it checks for a post argument and arguments posted via the url, get arguments, aren't valid as input.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.