Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I'm trying to create a Number Guessing Game. Everything works, except that I can't get to it count number tries the person gets. Here is the code: Code: <?php ...
  1. #1
    Just Joined! vitalka's Avatar
    Join Date
    Oct 2004
    Location
    Toronto, ON
    Posts
    77

    PHP - Variable Problems

    Hi, I'm trying to create a Number Guessing Game. Everything works, except that I can't get to it count number tries the person gets.

    Here is the code:
    Code:
    <?php
    session_start();
    
    if (isset($_SESSION['random']) && isset($_SESSION['try'])){
    	action();
    } else {
    	check_ses();
    	action();	
    }
    
    function action(){
    	if ($_POST['action']=="submit"){
    		$usr_num = $_POST['usr_num'];
    				compare_num();
    	} else {
    		guess_form();	
    	}
    }
    
    function check_ses(){
    	$random = rand(1,10000);
    	$_SESSION['random'] = $random;
    	$_SESSION['try'] = 11;
    	$try = $_SESSION['try'];
    	echo $try;
    }
    	
    function compare_num(){
    	if ($_POST['try'] == 1){
    		echo "You ran out of tries.";
    	} elseif ($_SESSION['random'] > $_POST['usr_num']){
    		echo "Higher!<br />";
    		echo "You have entered: " . $_POST['usr_num'] . "<br />";
    		echo "Number of tries left: " . $try = $_POST['try']-1;
    		guess_form();	
    	} elseif ($_SESSION['random'] < $_POST['usr_num']){
    		echo "Lower!<br />";
    		echo "You have entered: "  . $_POST['usr_num'] . "<br />";
    		echo "Number of tries left: " . $try = $_POST['try']-1;
    		guess_form();
    	} else {
    		echo "You Win! " . $_POST['usr_num'];
    		unset($_SESSION['random']);
    		unset($_SESSION['try']);
    	}
    }
    
    function guess_form(){
    	echo "<form id=\"form\" name=\"form\" method=\"POST\" action=\"lab2_4.php\">
    	<p> Guess a number between 1 and 10000:</p>
    	<p>
    	<input type=\"text\" name=\"usr_num\"	id=\"usr_num\" size=\"5\"	maxlength=\"5\" />	
    	</p>
    	<input type=\"hidden\" id=\"try\" name=\"try\" value=\"". $try ."\" />
    	<input type=\"hidden\" id=\"action\" name=\"action\" value=\"submit\" />
    	<input type=\"submit\" value=\"submit\" id=\"action\"/>			 
    	</form>";	
    }
    ?>
    I've set up a hidden field in HTML that would show me a number of tries left, but the value never gets passed on from the session variable.

    Any ideas as to what am i doing wrong?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    My PHP is very rusty...

    But you have $try inside a function. Unless it's declared a global variable, you have to pass the value out of the function.

    For security (and good coding practices), php.ini usually has global variables disabled by default.

    Not 100% that is your only issue, but may be something to start with.

  3. #3
    Just Joined! vitalka's Avatar
    Join Date
    Oct 2004
    Location
    Toronto, ON
    Posts
    77
    Thanks for the reply HROAdmin26.
    Well I worked around it. I decided to use a session variable for 'tries counter' instead. Everything seems to be working.

    Here is the code (for those who wants to try it out):
    Code:
    <?php
    session_start();
    
    if (isset($_SESSION['random']) && isset($_SESSION['try'])){
    	action();
    } else {
    	check_ses();
    	action();	
    }
    
    function action(){
    	if ($_POST['action']=="submit"){
    		$usr_num = $_POST['usr_num'];
    		compare_num();
    	} else {
    		echo "You have 10 tries left";
    		guess_form();	
    	}
    }
    
    function check_ses(){
    	$random = rand(1,3);
    	$_SESSION['random'] = $random;
    	$_SESSION['try'] = 9;
    }
    	
    function compare_num(){
    	if ($_SESSION['random'] == $_POST['usr_num']){
    		echo "You Win! " . $_POST['usr_num'] ."<br />";
    		unset($_SESSION['random']);
    		unset($_SESSION['try']);
    		echo "<a href=\"./lab2_4.php\" target=\"_self\" >Try again?</a>";
    	} elseif (!is_int($_POST['usr_num'])){
    		echo "You can only use integer numbers!";
    		guess_form();	
    	} elseif ($_POST['usr_num'] < 1 || $_POST['usr_num'] > 10000){
    		echo "You can only use numbers between 1 and 10000!";
    		guess_form(); 
    	} elseif ($_SESSION['try'] <= 0){
    		echo "You loose! <br /> You ran out of tries! <br />";
    		unset($_SESSION['random']);
    		unset($_SESSION['try']);
    		echo "<a href=\"./lab2_4.php\" target=\"_self\" >Try again?</a>";
    	} elseif ($_SESSION['random'] > $_POST['usr_num']){
    		echo "Higher!<br />";
    		echo "You have entered: " . $_POST['usr_num'] . "<br />";
    		echo "Number of tries left: " . $_SESSION['try']--;
    		guess_form();	
    	} elseif ($_SESSION['random'] < $_POST['usr_num']){
    		echo "Lower!<br />";
    		echo "You have entered: "  . $_POST['usr_num'] . "<br />";
    		echo "Number of tries left: " . $_SESSION['try']--;
    		guess_form();
    	} else {
    		echo "If you see this message, then something is broken.";
    	}
    }
    
    function guess_form(){
    	echo "<form id=\"form\" name=\"form\" method=\"POST\" action=\"lab2_4.php\">
    	<p> Guess a number between 1 and 10000:</p>
    	<p>
    	<input type=\"text\" name=\"usr_num\"	id=\"usr_num\" size=\"5\"	maxlength=\"5\" />	
    	</p>
    	<input type=\"hidden\" id=\"action\" name=\"action\" value=\"submit\" />
    	<input type=\"submit\" value=\"submit\" id=\"action\"/>			 
    	</form>";	
    }
    ?>

    If you will find anything unusual, please post here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...