Number Guessing game PHP not functoning -


i trying write php game raises random ($secretnumber) number between 1 , 100, prints screen, prints form buttons user select either 'higher' 'lower' or 'correct'. upon button press, outside values altered accordingly , computer raises random number between lowest , highest values. continues until user select correct , game resets. using session ensure values don't reset each time button clicked. problem $secretnumber resets couple of times before $_session locks in. $randomguess doesn't seem lock in before updating.

any suggestions why isn't working?

<?php session_start() ?>  <?php $low = 1; $high = 100; //create function hold random number, between 1 , 100  , save  //to session variable. secretnumber();  function secretnumber(){ //********** secretnumber() **********  global $randomguess;         if(!isset($_session["secretnumber"])){         $secretnumber = rand(1,100);         $_session['secretnumber'] = $secretnumber;     } else {         $secretnumber = $_session["secretnumber"];         echo($_session['secretnumber']);     }      echo 'secret number '.$secretnumber;     echo '  <br/>  ';     }                   //********** end secretnumber() ***********      //create function create random guess value of      //first random number  randomguess();  function randomguess() { //********** randomguess() **********  global $randomguess;  global $low;  global $high;  if (isset($_session['low'])){         $low = $_session['low']; }  if (isset($_session['high'])){         $high = $_session['high']; }     $randomguess = round($high-(($high-$low)/2));     echo 'random guess '.$randomguess;     echo "<br/>";     $_session['randomguess'] = $randomguess;     }                   // ********** end randomguess() **********  //create function provide response whether guess low,  //high or correct, , adjust limits of random guess accordingly.  checkguess();  function checkguess() { // ********** checkguess() **********  global $low;  global $high;  global $randomguess;  // form show 'high' button, 'low' button , 'correct' button // correct button needs reset $_session["secretnumber"], , start  // process over.      print <<<here         <div>                 <br/><br/><br/>                 <form method = "post" action = ""  style="width:35%;         padding-left:35%">                     <fieldset>                          <button type = "submit" value="high" name ="high">too high</button> <br/><br/>                         <button type = "submit" value="low" name = "low">too low</button> <br/><br/>                         <button type = "submit" value="correct" name = "correct">correct</button> <br/><br/>                      </fieldset>                 </form>             </div>   here;      if (isset($_post['low'])) {                 echo "low <br/>";                 echo "randomguess = ".$randomguess. "<br/>";                 unset($_session["low"]);                 $low = $randomguess;                 $_session['low'] = $low;                 unset($_session["randomnumber"]);     }     if (isset($_post['high'])){                 echo "high <br/>";                 echo "randomguess = ".$randomguess. "<br/>";                 unset($_session["high"]);                 $high = $randomguess;                 $_session['high'] = $high;                 unset($_session["randomnumber"]);     }  } //                   ********** end checkguess() **********  if (isset($_post['correct'])){             print <<<here             <h2>yes guessed correct secret number. secret out</h2>     here;         unset($_session["secretnumber"]);// unset random number guess correct.             unset($_session["high"]);             unset($_session["low"]);         }//end if  echo "low =".$low."<br/>"; echo "high =".$high." <br/>";  ?> 

try check if $secretnumber same session.

var_dump($secretnumber);

if($secretnumber != $_session["secretnumber"]) { echo "restart here"; }

test after every function , can see problem.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -