[PHP] Number Game

Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Valued Member NastyDee is offline
    MemberRank
    Oct 2008 Join Date
    BelgiumLocation
    114Posts

    [PHP] Number Game

    Hey,

    First script i made 100 % by myself so some tips would be great :D

    Check it here:

    http://down-street.nl/numbergame/

    Scripts:

    Index.html

    Code:
    <html>
    	<head>
    	<title>Number Game</title>
    	</head>
    		<body>
    			<form method="post" action="uitkomst.php">
    			Enter a number between 1 and 10! <br />
    			<input type="text" name="getal"> <br />
    			<input type="submit" value="GO!">
    			</form> 
    		</body>
    </html>
    Uitkomst.php

    PHP Code:
    <?php
    $number 
    mt_rand(1,10); 

    if(
    $_POST["getal"] == $number) {
        Echo 
    "Congratulations! The number was {$number} ";

    elseif(
    $_POST["getal"] > 10) {
        echo 
    "Please enter a number between 1 & 10 not higher!";

    else {
        echo 
    "Sorry, you took number {$_POST["getal"]} and the right number was {$number}!";
    }
    ?>


  2. #2
    The Omega Superfun is offline
    MemberRank
    Dec 2006 Join Date
    The NetherlandsLocation
    5,227Posts

    Re: [PHP] Number Game

    How about
    PHP Code:
    is_numeric($_POST['getal']) 

  3. #3
    Valued Member NastyDee is offline
    MemberRank
    Oct 2008 Join Date
    BelgiumLocation
    114Posts

    Re: [PHP] Number Game

    Quote Originally Posted by Superfun View Post
    How about
    PHP Code:
    is_numeric($_POST['getal']) 
    Can you also explain me whats the different and etc ?

  4. #4
    Account Upgraded | Title Enabled! shadow-xx is offline
    MemberRank
    Jan 2007 Join Date
    MegatonLocation
    804Posts

    Re: [PHP] Number Game

    is_numeric($var) would check the variable $var if it contains numbers or not
    if it does it will return true

    More info > http://php.net/is_numeric

    Ur not checking the data.. running this on a server could be unsafe (xss exploits)

  5. #5
    Valued Member NastyDee is offline
    MemberRank
    Oct 2008 Join Date
    BelgiumLocation
    114Posts

    Re: [PHP] Number Game

    hmm k so ehm, i'm just a beginner so don't really get it ^^

    well i kinda get it but, do i gotta add these in my if else statement? Or where ? sorry for being noob haha :D

    Edit: Hmm i think i'm gettin it, but i'm still a bit confused how to use this so can someone help me out ^^

  6. #6
    Account Upgraded | Title Enabled! shadow-xx is offline
    MemberRank
    Jan 2007 Join Date
    MegatonLocation
    804Posts

    Re: [PHP] Number Game

    Example :
    PHP Code:
    <?php
    $num 
    mt_rand(1,10); 

    if(
    is_numeric($_POST['num'])) {
        if (
    $_POST['num'] == $num) {
            echo 
    'Congratulations! The number was '.$num;
        }
        else {
             echo 
    'Sorry, you took number '.$_POST['num'].'and the right number was'.$num;
        }
    }
    else {
        echo 
    'Invalid Input, Please use numbers only.';
    }
    ?>

  7. #7
    Valued Member NastyDee is offline
    MemberRank
    Oct 2008 Join Date
    BelgiumLocation
    114Posts

    Re: [PHP] Number Game

    Quote Originally Posted by shadow-xx View Post
    Example :
    PHP Code:
    <?php
    $num 
    mt_rand(1,10); 

    if(
    is_numeric($_POST['num'])) {
        if (
    $_POST['num'] == $num) {
            echo 
    'Congratulations! The number was '.$num;
        }
        else {
             echo 
    'Sorry, you took number '.$_POST['num'].'and the right number was'.$num;
        }
    }
    else {
        echo 
    'Invalid Input, Please use numbers only.';
    }
    ?>
    Thanks man i'll keep this in mind

  8. #8
    Member Guildjuhh is offline
    MemberRank
    Jan 2009 Join Date
    65Posts

    Re: [PHP] Number Game

    Sorry, you took number -20and the right number was4

    You should check for negative numbers too ;)

  9. #9
    Account Upgraded | Title Enabled! MjClarke1 is offline
    MemberRank
    Jun 2006 Join Date
    London, OntarioLocation
    644Posts

    Re: [PHP] Number Game

    And check for numbers over 10 ;)

  10. #10
    Account Upgraded | Title Enabled! CobraCom is offline
    MemberRank
    Jul 2008 Join Date
    382Posts

    Re: [PHP] Number Game

    Try using this.

    - You can describe any error, and define it as $error before you start the script. If there's an $error defined before the script starts,
    it displays the error.

    Code:
    if (!is_numeric($_GET['number']){
          $error = "You did not enter a number";
    }
    elseif ($_GET['number'] > 11 || $_GET['number'] < 0){
          $error = "You've enter a number larger than 11, or smaller than 0";
    }
    
    if (empty($error)){
          $winning_number = rand(1, 10);
          $user_numer = $_GET['number'];
                if ($winning_numer == $user_number){
                      echo "You won!";
                } else {
          echo "You entered ".$user_numer.", the winning number was ".$winning_numer;
          }
    }
    else {
    echo $error;
    }

  11. #11
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP] Number Game

    Quote Originally Posted by CobraCom View Post
    Try using this.

    - You can describe any error, and define it as $error before you start the script. If there's an $error defined before the script starts,
    it displays the error.
    Nice, now let's make it show all possible errors all at once, where we want them to show. (Pro-Learning)
    PHP Code:
    <?php
    // Show Directions
    $message =  '<p>Enter a number between 1 and 10!</p>';

    //Check if button was clicked. [or if form was sent]
    if(isset($_GET['submit']))
    {
        
    //Clear Directions
        
    $message ''

        
    //Check if it's a number
        
    if (!is_numeric($_GET['getal']))
        {
            
    $message .= '<p>Warning: * You did not enter a number.</p>';
        }
        
        
    //Greater than 0, less than 11?
        
    if ($_GET['getal'] > 10 || $_GET['getal'] < 1)
        {
            
    $message .= "<p>Warning: * You've entered a number larger than 10, or smaller than 1.</p>";
        }
        
        
    //Okay, no errors = continue.
        
    if (strlen($message)<1)
        {
            
    $winning_num rand(110);
            
    $user_num $_GET['getal'];
            
            
    //User wins :)
            
    if ($winning_num == $user_num)
            {
                
    $message.='You won!';
            }
            
    //User Loses :(
            
    else
            {
                
    $message.='You entered '.$user_num.', the winning number was '.$winning_num;
            }
        }
    }


    ?>
    <form method="get" action="index.php">
        <?php echo $message?>
        <br />
        <input type="text" name="getal" value="<?php echo $_GET['getal']; ?>"> <br />
        <input type="submit" name="submit" value="GO!">
    </form>
    Note: if you define a variable using ".=" the variable will hold it's current value, and add onto it with the new value. That's how we combine all of the possible error messages.

    Take this example:
    PHP Code:
    $string 'hello, ';
    $string .= 'hello';
    echo 
    $string//returns 'hello, hello' //Off Topic: I don't know why you say goodbye, I say hello. 
    Note: strlen($str) returns the length of $str.

    So a string with the value 'abcdefg' will return 7. A string with the value '45678' returns 5.

    If the length of the submit value is less than 0, or if the length of $message is greater than 0, halt the execution of the script. Why? Because that means the form was never sent, or there was an error. You don't want a warning message displayed before the form was used. Equally, you wouldn't want the script to be executed unless the form was submitted with no errors. So we ask if the form was submitted before checking for errors, than we check if $message is greater than 0. If it is, it means there's an error, so forget the calculations and show the error.

    Yeah, I was bored...

    Just include the two scripts onto the same file, or join them all in one file idc
    Last edited by s-p-n; 25-05-09 at 09:39 PM. Reason: debugged

  12. #12
    Account Upgraded | Title Enabled! CobraCom is offline
    MemberRank
    Jul 2008 Join Date
    382Posts

    Re: [PHP] Number Game

    Why use strlen to check if the string is 0 chars? just checking if it's not empty reduces the server load time.

  13. #13
    The Omega Superfun is offline
    MemberRank
    Dec 2006 Join Date
    The NetherlandsLocation
    5,227Posts

    Re: [PHP] Number Game

    Quote Originally Posted by CobraCom View Post
    Why use strlen to check if the string is 0 chars? just checking if it's not empty reduces the server load time.
    or
    PHP Code:
    if

    elseif

    else 
    which makes loading even faster

  14. #14
    Account Upgraded | Title Enabled! CobraCom is offline
    MemberRank
    Jul 2008 Join Date
    382Posts

    Re: [PHP] Number Game

    Quote Originally Posted by Superfun View Post
    or
    PHP Code:
    if

    elseif

    else 
    which makes loading even faster
    yup. By using comon knowledge you can rule out statements to be checked, so the server won't have to check them if an error already occured.

  15. #15
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP] Number Game

    Quote Originally Posted by CobraCom View Post
    Why use strlen to check if the string is 0 chars? just checking if it's not empty reduces the server load time.
    Using strlen($string) is the same as using string.length - which is somewhat of a standard in checking if a string is empty or not.

    For example, if(string.length>0) grant access to this script;

    I tend to use cross compatible tactics because it works in every language, maybe a bit differently- and there's no difference in performance that I know of.

    Here's a little test for ya,
    PHP Test Code:
    PHP Code:
    <?php
    function microtime_float()
    {
        list(
    $usec$sec) = explode(" "microtime());
        return 
    substr(((float)$usec + (float)$sec),0,15);
    }
    $str_1000 = (string) '1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111';
    $str_1 = (string) null;
    $int_0 = (int) 0;


    $start microtime_float();

    $test = isset($int_0);

    $end microtime_float();
    echo 
    '<p>Int 0: '.$int_0.'
        <br />
        Is Set: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.' <br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;

    $start microtime_float();

    $test = isset($str_1);

    $end microtime_float();
    echo 
    '<p>String 1: '.$str_1.'
        <br />
        Is Set: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.' <br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;
        
    $start microtime_float();

    $test = isset($str_1000);

    $end microtime_float();
    echo 
    '<p>String 1000: '.$str_1000.'
        <br />
        Is Set: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.' <br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;


        
    $start microtime_float();

    $test strlen($int_0);

    $end microtime_float();
    echo 
    '<p>Int 0: '.$int_0.'
        <br />
        String Length: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.'<br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;

    $start microtime_float();

    $test strlen($str_1);

    $end microtime_float();
    echo 
    '<p>String 1: '.$str_1.'
        <br />
        String Length: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.'<br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;

    $start microtime_float();

    $test strlen($str_1000);

    $end microtime_float();
    echo 
    '<p>String 1000: '.$str_1000.'
        <br />
        String Length: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.'<br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;

    $start microtime_float();

    $test = empty($int_0);

    $end microtime_float();
    echo 
    '<p>Int 0: '.$int_0.'
        <br />
        Empty: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.' <br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;
        
    $start microtime_float();

    $test = empty($str_1);

    $end microtime_float();
    echo 
    '<p>String 1: '.$str_1.'
        <br />
        Empty: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.' <br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;
        
    $start microtime_float();

    $test = empty($str_1000);

    $end microtime_float();
    echo 
    '<p>String 1000: '.$str_1000.'
        <br />
        Empty: '
    .$test.'<br />
        Start: '
    .$start.'<br />
        End: '
    .$end.' <br />
        Duration: '
    .substr(($end-$start),0,10).'</p>
        
        '
    ;
    ?>
    My Result: [Results may vary, of course ;)] Normally it's 0 for all of them, so they all perform the same, no matter if there's 1000 characters or 0. I even tried it with 100k! Note that I'm not logging the time it takes to set the variable; which does of course effect load time.

    With this test: I'm using 3 different predefined variables under 3 different methods.

    You should always predefine variables. Therefore: Don't use isset() to check existing variables.
    *Does not recognize null as empty.

    Isset returns 1 for null.
    isset returns 1 for 0
    isset returns 1 for 1k 1's.

    Checks String Length for ints/strings.
    *Recognizes null as empty.

    strlen returns 0 for null.
    strlen returns 1 for 0.
    strlen returns 1000 for 1k 1's.

    Works for strings, works for ints, but returns empty if the int is 0.
    *Recognizes null as empty.

    empty returns 1 for null.
    empty returns 1 for 0.
    empty returns null for 1k 1's.

    I see nothing against calling empty() the best solution.

    It's just habit for me to use strlen($str), & str.length;there is no disadvantage imo..
    Code:
        <p>Int 0: 0
        <br />
        Is Set: 1<br />
        Start: 1243286735.2258<br />
        End: 1243286735.2258 <br />
        Duration: 0</p>
        
        <p>String 1: 
        <br />
        Is Set: 1<br />
        Start: 1243286735.2259<br />
        End: 1243286735.2259 <br />
        Duration: 0</p>
        
        <p>String 1000: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
        <br />
        Is Set: 1<br />
        Start: 1243286735.2259<br />
        End: 1243286735.2259 <br />
        Duration: 0</p>
        
        <p>Int 0: 0
        <br />
        String Length: 1<br />
        Start: 1243286735.226<br />
        End: 1243286735.226<br />
        Duration: 0</p>
        
        <p>String 1: 
        <br />
        String Length: 0<br />
        Start: 1243286735.226<br />
        End: 1243286735.2261<br />
        Duration: 9.98973846</p>
        
        <p>String 1000: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
        <br />
        String Length: 1000<br />
        Start: 1243286735.2261<br />
        End: 1243286735.2261<br />
        Duration: 0</p>
        
        <p>Int 0: 0
        <br />
        Empty: 1<br />
        Start: 1243286735.2262<br />
        End: 1243286735.2262 <br />
        Duration: 0</p>
        
        <p>String 1: 
        <br />
        Empty: 1<br />
        Start: 1243286735.2262<br />
        End: 1243286735.2262 <br />
        Duration: 0</p>
        
        <p>String 1000: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
        <br />
        Empty: <br />
        Start: 1243286735.2263<br />
        End: 1243286735.2263 <br />
        Duration: 0</p>
    I do agree with the else if() point, that's very true.



Page 1 of 2 12 LastLast

Advertisement