0

Hi,

to my understanding, isset() checks is the variable exist and have a value. But when I try the code below, _randNum() keeps on getting new values when it should already have a value because it already exist and have a value.

<?php
    session_start();
    if(!isset($_randNum)){ //Check if the variable dont exist 
        $_randNum = rand (10, 20); //if not
    }
    $rNum = $_randNum;
?>

I have a form to check the value in $rNUm:

    <form method="post" action="guessinggame.php">
        <p><input type="text" name="tb" id="tb" />
        <input type="submit" name="guess" id="guess" value="Guess" /></p>
    </form>
    <?php
        echo "$rNum";
    ?>

This is my first php so i'm not really sure whats going on.

2
Contributors
1
Reply
14
Views
2 Years
Discussion Span
Last Post by hericles
1

Although you are setting a session you aren't putting anything into, or passing values via the form.
This means that on each page reload $_randNum isn't set as the page is 'new' and has no variables being passed to it or being retrieved from the session.
To add the rand number to the session:
$_SESSION['rand'] = $_randNum;
To retrieve:
if(!isset($_SESSION['rand']))

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.