Generating a random number in php class

I am doing an exercise I found online to teach myself php

. The program I am creating is a guess the number game in which I have to create a random number that I have to guess. I was successful in the exercise where I didn't use the class and used input fields html

for user input:

<?php
if(isset($_POST['number'])) {
    $number = $_POST['number'];
} else {
    $number = rand(1,99);
}

if($_POST["guess"]) {
    // grab the 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 it in " . $count . " trials";
    }
} ?>


<!DOCTYPE html>
<html>
    <head>
        <title>Guess A Number</title>
    </head>

    <body>
        <form action="NumberGuess.php" method="post">
            <label for="guess">Guess A 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, now I want to repeat the exercise using json

as input and using classes / objects. I tried using rand()

to generate a random number, however this causes a problem and the page does not 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 . " is incorrect, " . "try guessing " . $this->higher;
        } elseif($this->isNumber == true) {
            return $this->guess . " is 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());

      

Can anyone explain to me how I can generate a random number in a class using json

?

+3


source to share


1 answer


You can use PHP

rand () or mt_rand () to generate a random number between the 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. You can use JavaScript

Math.random () to generate a random number instantly. Or you can use Ajax

to generate a random number with PHP

.

+1


source







All Articles