json - Generate a random number in a php class -


i doing exercise found online teach myself php. program making "guess number" game need generate random number guessed. successful in exercise when not using class , using html input boxes users input:

<?php if(isset($_post['number'])) {     $number = $_post['number']; } else {     $number = rand(1,99); }  if($_post["guess"]) {     // grab user input guess     $guess  = $_post['guess'];     $number  = $_post['number'];     $output = '';     static $count = 0; //initialize count      if ($guess < $number) {          $output .= "$guess" . "<br />" . "guess higher";         $count++;     } elseif($guess > $number) {                $output .= "$guess" . "<br />" . "guess lower";         $count++;     } elseif($guess == $number) {           $count++;             $output .= "$guess" . "<br />" . "you got it!" . "<br />" . "you guessed in " . $count . " trials";     } } ?>   <!doctype html> <html>     <head>         <title>guess number</title>     </head>      <body>         <form action="numberguess.php" method="post">             <label for="guess">guess number:</label><br/ >             <input type="text" name="guess" />             <input name="number" type="hidden" value="<?= $number ?>" />             <input name="submit" type="submit" />             <br/>             <?php echo $output ?>             <br/>         </form>     </body> </html> 

however, want repeat exercise using json input , using classes/objects. tried using rand() generate random number, causes issue , page won't load:

class numberguess {     protected $guess = 0;     const randnumber = rand(1,99);     protected $isnumber = false;     protected $higher = true;      function __construct($inputguess) {         $this->guess = $inputguess;     }      public function getresult() {         $this->numberchecker();          if($this->higher == true) {             $this->higher = "higher";         } elseif($this->higher == false) {             $this->higher = "lower";         }          if($this->isnumber == false) {             return $this->guess . " incorrect, " . "try guessing " . $this->higher;         } elseif($this->isnumber == true) {             return $this->guess . " correct!";         }     }      private function numberchecker() {         if($this->guess < self::randnumber) {             $this->higher = true;             $this->isnumber = false;         } elseif($this->guess > self::randnumber) {             $this->higher = false;             $this->isnumber = false;         } elseif($this->guess == self::randnumber) {             $this->higher = false;             $this->isnumber = true;         }     } }  if ( isset($_server['query_string']) ) {     $inputstring = $_server['query_string'];     $inputguess = intval($inputstring); }  $myguess = new numberguess($inputguess); echo json_encode($myguess->getresult()); 

could please explain me how can generate random number in class using json?

you can use php rand() or mt_rand() function generate random number between range.

rand() syntax

int rand ( void ) #or int rand ( int $min , int $max ) 

mt_rand() syntax

int mt_rand ( void ) #or int mt_rand ( int $min , int $max ) 

note : can use javascript math.random() function instant generate random number. or can use ajax generate random number using php.


Comments