PHP猜数

This is a random number guessing game between 1 and 100.

I am stuck on the guessing number part. The code doesn't work, whatever number I put it just show me the message "Your number is lower!".I need help.

This is what I have so far.

<?php

$x   = rand(1, 100);
$num = '';

if (isset($_POST['submit'])) {


        if ($num < $x) 
        {
            echo " Your number is lower! <br />";
        } elseif ($num > $x) 
            {
            echo " Your number is higher! <br />";
        } elseif ($num == $x) 
            {
            echo " Congratulations! You guessed the hidden number. <br />";
        } else 
            {
            echo " You never set a number! <br />";
        }




}
?>
<p>
<form action="" method="post">
<input type="text" name="num">
<button type="submit" name="submit">Guess</button>
<button type="reset" name="Reset">Reset</button>
</form>
</p>

You will always get lower because There isn't any value inside you $num.So, You need to assigning $num

 <?php

    $x   = rand(1, 100);
    $num = '';

    if (isset($_POST['submit'])) {
     $num = $_POST['num']; // Add this to set value for $num variable
            if ($num < $x) 
            {
                echo " Your number is lower! <br />";
            } elseif ($num > $x) 
                {
                echo " Your number is higher! <br />";
            } elseif ($num == $x) 
                {
                echo " Congratulations! You guessed the hidden number. <br />";
            } else 
                {
                echo " You never set a number! <br />";
            }
    }
    ?>

You need to set the num after submit the form.

if (isset($_POST['submit'])) {
    $num = $_POST['num'];
    if ($num < $x) 
    {
       echo " Your number is lower! <br />";
    } elseif ($num > $x) 
    {
        echo " Your number is higher! <br />";
    } elseif ($num == $x) 
    {
        echo " Congratulations! You guessed the hidden number. <br />";
    } else 
    {
        echo " You never set a number! <br />";
    }
}
// value of $x is in between 1 and 100 means greater than 0 and lesser than 101
$x   = rand(1, 100);
// $num value is empty and while you attempt to compare with this this will converted to zero
$num = '';
// thus $num<$x always returns true, thats the mistake you need to assign the submitted value to the $num variable like  
$num=$_POST['num'];
// put above line just before the if($num<$x)

This would be a lot easier if you had it as a switch/case statement:

$x = rand(1,1000);
if(isset($_POST['submit'])) {
    $num = $_POST['num'];
    switch($num) {
        case ($num < $x):
        echo " Your number is lower! <br />";
        break;
        case ($num > $x):
        echo " Your number is higher! <br />";
        break;
        case ($num == $x):
        echo " Congratulations! You guessed the hidden number. <br />";
        break;
        case '':
        echo " You never set a number! <br />";
        break;
    }
}

Example/Demo

You have set $num = '' (a fix empty string) and compare it with a random generated number. It's obvious that this can not work. You have to read $_POST['num'] in PHP.

This will work, however, not as probably expected. You will need to either store the random number in a session or easier add a hidden field to the form. Otherwise the random number will change on each try.