Thread: [PHP/HTML] Simple Number Guessing

Results 1 to 6 of 6
  1. #1 [PHP/HTML] Simple Number Guessing 
    Registered Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Just thought I'd post this simple little script for others to learn from, just kind of brushing up on PHP mostly and wrote this along the way.
    It enables you to set a number for people to guess between 1-10 or what have you.. customize it to your liking.

    Save this as a .PHP file in your web directory:
    Code:
    <html>
    <body style="text-align:center;">
    <b>Guess a number between 1 and 10.</b>
    <br />
    <form action="" method="post">
        <input type="text" name="guess" maxlength="2" />
        <input type="submit" name="submit" value="Guess!" />
    </form>
    
    <?php
    $WinningNumber = "7";    /* Change this to your winning number */
    if(isset($_POST['submit'])) {    /* Makes sure the script is executed upon clicking the submit button */
        $Guess = $_POST['guess']; /* This variable grabs the players guess */
        if($Guess < $WinningNumber) {    /* Scenario if the guess was lower than the winning number */
            echo "Nope. Higher!";    /* Statement hinting to guess higher */
        } elseif($Guess > $WinningNumber) { /* Scenario if the guess was higher than the winning number */
                echo "Nope. Lower!";    /* Statement hinting to guess lower */
        }
        else {
            echo "We have a winner! " . $WinningNumber . " is correct!!!"; /* Winning statement and number isnt higher or lower */
        }
    }
    ?>
    
    </body>
    </html>
    Let me know if there's any issues, haven't tested it, i just trust in myself.

    *Note*
    Remove all of the "/*ADADADA*/" comments next to the lines of code if you do not wish to have them.

    ~Singod
    Reply With Quote  
     

  2. #2  
    q.q


    Join Date
    Dec 2010
    Posts
    6,519
    Thanks given
    1,072
    Thanks received
    3,535
    Rep Power
    4752
    this would not work

    Code:
    if($Guess < $WinningNumber) {
            echo "Nope. Higher!";
             } elseif($Guess > $WinningNumber) {
                echo "Nope. Lower!";
            }
        else {
            echo "We have a winner! " . $WinningNumber . " is correct!!!";
        }
    missing a }

    perhaps comment it if you want others to learn from it
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Quote Originally Posted by Harlan View Post
    this would not work

    Code:
    if($Guess < $WinningNumber) {
            echo "Nope. Higher!";
             } elseif($Guess > $WinningNumber) {
                echo "Nope. Lower!";
            }
        else {
            echo "We have a winner! " . $WinningNumber . " is correct!!!";
        }
    missing a }

    perhaps comment it if you want others to learn from it
    Good idea. And thanks for the correction! I've also added comments next to the important lines of function.

    ~Singod
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Jan 2011
    Posts
    1,940
    Thanks given
    1,217
    Thanks received
    547
    Rep Power
    607
    Why not randomize it instead of having a set number?
    Reply With Quote  
     

  5. #5  
    Donator

    Join Date
    Feb 2012
    Posts
    12
    Thanks given
    7
    Thanks received
    2
    Rep Power
    11
    $Guess will throw an undefined index, put the $Guess var inside of the if statement
    Reply With Quote  
     

  6. #6  
    Donator
    Intredy's Avatar
    Join Date
    Sep 2011
    Age
    25
    Posts
    219
    Thanks given
    19
    Thanks received
    25
    Rep Power
    15
    I made it a little more straight forward for ya.
    Code:
    <?php
    // If "Guess" is pressed
    if(isset($_POST['submit'])) {
        // Checks if guess field is empty
        if($_POST['guess'] == "") {
            // If empty, outputs this string
            echo "Please fill in the field.";
        } else {
            // Checks if it's a number or not
            if((int) $_POST['guess']) {
                // Generates number
                $number = rand(1, 10);
                // Sets the guessed number as a variable
                $guess = (int) $_POST['guess'];
                // Compares guess to the generated number
                if($guess != $number) {
                    // If incorrect, outputs this string
                    echo "Incorrect number, you guessed " . $guess . ". " . $number . " was the correct number";
                } else {
                    // If correct, outputs this string
                    echo "We have a winner! " . $number . " is correct!";
                }
            } else {
                // If it's not a number, outputs this string
                echo "Please enter a number";
            }
        }
    }
    ?>
    I removed the higher lower stuff because it's randomly generated.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Looking for php/html/css coder
    By Layer in forum Application Development
    Replies: 2
    Last Post: 01-11-2012, 09:15 AM
  2. Project | PHP | HTML | MySQL
    By Kyle Tulau in forum Website Development
    Replies: 11
    Last Post: 10-22-2011, 03:08 AM
  3. PHP HTML help
    By Gothickidd in forum Application Development
    Replies: 13
    Last Post: 09-10-2011, 05:46 PM
  4. [Request] How to make a basic PHP/HTML page on VB 4, using vBAdvanced
    By Zee Best in forum Application Development
    Replies: 1
    Last Post: 11-28-2010, 03:52 AM
  5. Replies: 11
    Last Post: 07-07-2010, 11:47 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •