r/PHPhelp • u/bbez69 • Oct 12 '21
Solved Beginner PHP
Hey guys,
So I tried to make a code which generates a random number then the user has to guess a number then the code tells the user if the randomly generated number is lower or higher.
And there is one more button where the user can generate a new random number but I get an error which says " Undefined variable $rand".
I tried to make this code in a session but I couldn't do it because it always said in the end " array(1) { ["random"]=> int(89) }", that's why I have the var_dump in the end.
I'm just confused how to do it.
<FORM name="form1" method="POST" action="játék.php">
Random <INPUT type="text" name="random"><BR>
<INPUT type="submit" name="submit" value="Submit"><BR>
<INPUT type="submit" name="roll" value="Random number generator"><BR>
</FORM>
<?php
session_start();
if (isset($_POST['roll'])) {
$rand = rand(1, 100);
echo $rand.'<BR>';
} elseif ($_POST['random'] > $rand) {
echo 'Lower';
} elseif ($_POST['random'] == $rand){
echo 'Bingo!';
} else {
echo 'Higher';
}
echo '<PRE>';
var_dump($_SESSION);
echo '<PRE>';
?>2
u/ThreeForksNoSpoon Oct 12 '21
Making a game like this is very good practice when learning PHP. Good job!
There are a couple of things you want to do:
You want to use a session, correct. What has probably happened here is that you put the random number in a session at some point (the 89), but never changed it after, so it stays at 89. Let's forget that for now.
You want to check if the request method is POST (that means the form is posted), before you do anything else. If not, you can just stop the script. So something like this:
if($_SERVER["REQUEST_METHOD" !== "POST"){ exit; }as the first line of your PHP (but after the form!).That's a quick-and-dirty one-liner, just to get you going for a bit. ;)
If the form is POST-ed, you want to check if the user has pressed the "roll"-button. You already do that. But, you also want to check if there's a random number present.
If not, you probably want to generate it at the spot. So the if should become something like
if(isset($_POST['roll']) || !isset($_SESSION['random'))In that if you want to generate the random number, and put it in the session:
$_SESSION["random"] = rand(1,100);After the if, you want to start using
$_SESSION["random"], not$rand, because you want to read from the session
That should get you going in the right direction.
1
2
u/rbmichael Oct 12 '21
You'll want to put session_start() at the very top of the file, before any HTML output. This is because (unless you set up some kind of advanced output buffering, and I don't think you have) session_start() needs to send a header to the browser, and if regular output occurs, the header won't get sent to the user, and thus they won't get the cookie needed for the session. Hope i explained that okay, but go like this:
<?php
session_start();
// put any other starting code here that needs to happen before HTML output.
?>
<form ...>
...
</form>
<?php
// the rest of your code
?>Your code example should have generated an error, so if you didn't see it, you might want to check that you have E_ALL and display errors enabled (don't display errors in production though, just during development).
Example error (session_start() placed after output): https://3v4l.org/Pqmt5
Example no error (session_start placed before output): https://3v4l.org/LhQKk
0
Oct 12 '21
[deleted]
1
u/bbez69 Oct 12 '21
I did copy paste everything
2
u/CyberJack77 Oct 12 '21
The problem is this part of the code:
if (isset($_POST['roll'])) { $rand = rand(1, 100); echo $rand.'<BR>'; } elseif ($_POST['random'] > $rand) { ... } elseif ($_POST['random'] == $rand) { ... } else { ... }If you describe the logic it would say:
if $_POST['roll'] is defined, then create the
$randvariable and echo it".if $_POST['roll'] is not defined, check if $_POST['random'] is bigger than $random. If so, then echo "lower".
if $_POST['roll'] is not defined, and $_POST['random'] is not bigger than $random, check if $_POST['random'] is equal than $random. If so, echo "Bingo!"
If $_POST['roll'] is not defined, and $_POST['random'] is not bigger than $random, and $_POST['random'] does not equal $random, echo "Higer".
These elseif's are is not possible, because
$randdoes not exist. You define it only when $_POST['roll'] is defined, but it's not available to the other elseif's.Also, you never check if $_POST['random'] exists.
Try to extract the $random variable to outside the if structures. Also, try to cut down on the if/elseif/elseif/else structure. Use multiple if statements, a switch, or a match (when using PHP 8).
Example:
<?php session_start(); if (isset($_POST['roll']) { $_SESSION['random_number'] = random_int(1, 6); echo 'I have thrown: ' . $_SESSION['random_number']; exit; } if (isset($_POST['random']) { if (!isset($_SESSION['random_number'])) { echo 'The random number has not been decided yet!. Please thow the dice'; exit; } // Insert the logic to check $_SESSION['random_number'] against $_POST['random'] here }
4
u/flarmigan Oct 12 '21
You've defined $rand in the if(...){ .... } but have tried to use it in the else {....} ... You should define it before the IF