How to make a random generator with PHP

Make your own random generator with PHP and HTML

To code a random generator with PHP is quite easy. You will need to know the PHP and HTML basics, but I will help you with that as well. Follow this tutorial step by step, fill the arrays with your own content, and you have yourself a custom made random generator.

Ok, here we go:

Step 1: Come up with a concept for a generator

First you need an idea for what you want to generate, this is up to you. For inspiration check out one of our generators, like the festival name generator, or the dj name generator. In this example we will keep it simple and use a random name generator. This will generate a random first name and a random last name and put this together.

Step 2: Create arrays

An array is a list of items, wich we can fill with words. In this example we’ll make two arrays: 1 for the first names, 1 for the last names. Open a script-editor like Notepad, an create a new file.
Start your PHP script with

<?php

leave a few lines empty and end it with

?>

In between we can start making the arrays, like this:

<?php
array();
array();
?>

To make it easy for ourselves, let’s put each array in a variable, wich we can use later to call the array in an easy way. We name the variables “firstnames” and “lastnames”:

$firstnames = array();
$lastnames = array();

To fill the arrays with words, you need to put the list of words inside the ( ). Each word or sentence must be in between ” ” and seperated by a comma:

$firstnames = array("John", "Mike", "Will", "Pete", "Lucy", "Barbara");
$lastnames = array("Johnson", "Jackson", "Williams", "Smith");

Step 3: Make it random

To choose a random word out of the list you can use the “array_rand” function wich will pick a random item, and output the number of that item. After that we can use this number to grab the corresponding item out of the array. We will put this all in a variable as well, and call it “random_firstname” and “random_lastname”. The script will look like this:

$random_firstname = $firstnames[array_rand($firstnames)];
$random_lastname = $lastnames[array_rand($lastnames)];

Step 4: Echo the full generated name

To make the generated name visible, we need to use the echo function, to put the output in between HTML tags. the full script including all HTML tags will look like this:

<!DOCTYPE html>
<head></head>
<body>

<?php

$firstnames = array("John", "Mike", "Will", "Pete", "Lucy", "Barbara");
$lastnames = array("Johnson", "Jackson", "Williams", "Smith");

$random_firstname = $firstnames[array_rand($firstnames)];
$random_lastname = $lastnames[array_rand($lastnames)];

$fullname = $random_firstname . " " . $random_lastname;


?>

<h1>My first name generator</h1>
<p>This name is randomly generated: <strong><?php echo $fullname; ?></strong> </p>
</body>
</html>

Step 5: Save and upload the file

Save the file as a php file, for example: random.php
Upload it to your server with an sftp app, and open the file to check if it works.

Good luck, and feel free to ask any questions below!

Leave a Reply