0
<?php session_start(); ?>
$_SESSION['catRandNum'] = rand(0,2);

That is one of my variables randomly selecting a category for my hangman mini-game I am making. Before Refresh

After Refresh

Is there any way I could make the variable stay constant so the category will stay the same? Thank you for your help.

2
  • If you don't want to change it, don't reassign it. Commented Dec 31, 2013 at 19:02
  • 2
    Check if session variable already exists. If not, generate a new one, if it does, do nothing :) Commented Dec 31, 2013 at 19:02

1 Answer 1

4

maybe this is what you want:

if (!isset($_SESSION['catRandNum'])) {
    $_SESSION['catRandNum'] = rand(0,2);
}

If this is a new session, the variable is set randomly. Otherwise it keeps its value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.