Random Number Generator with PHP and HTML List Display

Generate a list of secure random numbers with PHP and display them in an ordered HTML list. User input is validated with a max limit of 100 numbers.

Brief Introduction

Random Number Generator with PHP and HTML List Display (php)
DESCRIPTION:
This PHP project generates a list of random numbers and displays them in an ordered HTML page. The user can specify how many numbers to generate via a web form. The code is simple, robust, and ready to use, ideal for learning how to handle user input, random number generation, and dynamic HTML output.

ADDITIONAL NOTES:
The code is written in a simple and clear way, with comments in Italian to facilitate understanding. The HTML form uses the GET method for simplicity. The random_int() function is used to generate secure and reliable random numbers. The maximum number of generated elements is limited to 100 to avoid excessive load.

EXPLANATIONS:
- Use of random_int() to generate secure and unpredictable random numbers.
- Sanitization and validation of user input to avoid errors or attacks.
- Maximum limit of 100 numbers to avoid performance or display issues.
- Separation between PHP logic and HTML output to keep the code readable.
- Comments in Italian to explain every important part of the code.

Example Code: php

<?php
// Set the default number of elements
$numeroElementi = 10;
// Maximum limit to avoid excessive load
$maxElementi = 100;

// Check if a value was sent via GET
if (isset($_GET['count'])) {
    // Sanitize and convert to integer
    $input = filter_input(INPUT_GET, 'count', FILTER_VALIDATE_INT, [
        'options' => [
            'default' => $numeroElementi,
            'min_range' => 1,
            'max_range' => $maxElementi
        ]
    ]);
    if ($input !== false) {
        $numeroElementi = $input;
    }
}

// Generate the array of random numbers
$numeriCasuali = [];
for ($i = 0; $i < $numeroElementi; $i++) {
    // Generate a random number between 1 and 1000
    $numeriCasuali[] = random_int(1, 1000);
}
?>

<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <title>Random Number Generator</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        input[type=number] { width: 60px; }
        ul { padding-left: 20px; }
    </style>
</head>
<body>
    <h1>Random Number Generator</h1>
    <form method="get" action="">
        <label for="count">How many numbers to generate? (1-100): </label>
        <input type="number" id="count" name="count" min="1" max="100" value="<?php echo htmlspecialchars($numeroElementi); ?>" required>
        <button type="submit">Generate</button>
    </form>

    <h2>Generated numbers:</h2>
    <ul>
        <?php foreach ($numeriCasuali as $numero): ?>
            <li><?php echo htmlspecialchars($numero); ?></li>
        <?php endforeach; ?>
    </ul>
</body>
</html>
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

PHP 7.0+ installed and configured on the server

Web server (e.g. Apache, Nginx) with PHP support

Web browser to view the page

DEPENDENCIES: no external libraries, only standard PHP

CONFIG: no external configuration required

Similar Topics

First PHP script: display a message on the screen
Learn how to create your first PHP script that displays a welcome message on the screen. Perfect for beginners to see immediate results with PHP code.
PHP page displaying the current date and time
Simple PHP script displaying current date and time in day/month/year hour:minute:second format with timezone settings for accurate local time display.
Very Simple Calculator with Two Numbers in PHP
A very simple PHP calculator script that adds two user-entered numbers via an HTML form and displays the result on the same page securely and efficiently.
PHP Form that Greets the User by Name
Simple PHP form that greets users by name using POST method with secure input handling and beginner-friendly code in a single PHP file.
PHP page that counts how many times it has been visited
Simple PHP script that counts and displays the number of visits to a web page using a writable text file to store the counter value securely.
Static product list displayed with PHP
Simple PHP example displaying a static product list with name description and price using arrays and foreach loop for beginners learning PHP.
PHP page that generates an HTML table from an array
Learn how to create a simple PHP page that dynamically generates an HTML table from a multidimensional array with secure and easy to understand code.
Mini address book in PHP with a contacts array
A simple PHP mini address book script using an array to display and search contacts by name with case insensitive search and no database needed.