Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:58:23+00:00 2026-06-07T16:58:23+00:00

In the following PHP code, I’m trying to generate a random number if its

  • 0

In the following PHP code, I’m trying to generate a random number if its not already and store it in a variable called $number, also I’m taking user input and storing it in $userInput.

Every time the submit button is pressed , the same page is called again using $_SERVER['PHP_SELF'].

My problem is that I want the $number to keep its value once its set. no matter how many times the submit button is pressed, which is something that is not happening now.
the $number is set every time I press submit button.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guessing Game!</title>
    </head>
    <body>
        <?php 
            $userInput = $_POST['number'];
            if(!$number){
                 $number = rand(1, 500);
            }
            else{
                 echo "random number is $number <br>";
                 echo "you entered $userInput";
            }

            echo "<h3> $number </h3>"; //just to see if the number is changing everytime
       ?>
       <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
            <input type="text" name="number" /> <input type="submit" name="Submit"
            value="Submit" />
       </form>
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:58:25+00:00Added an answer on June 7, 2026 at 4:58 pm

    To make your random number persist betwen page views, you’ll need to use sessions

    http://www.tizag.com/phpT/phpsessions.php/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP code, but it's not working. I don't see any
The following PHP code prints out this warning: Warning: openssl_csr_get_public_key(): supplied resource is not
I am using the following PHP code... $settings[] = get_app_settings(); echo $settings['weight_unit']; but its
I use the following PHP code to generate some JS and echo it in
I'm trying to port the following php code to javascript on node.js: $mac =
The following PHP code is working as expected. I need to echo copy error
I have the following PHP code, and for the life of me I can't
I have the following php code: $k=1; for($i=0; $i < 5; $i++) { $stmt
I have the following php code: $k=1; for($i=0; $i < 5; $i++) { $stmt
I have the following PHP Code #The facebook message $fh = fopen('comments.txt', 'r') or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.