Tutorial Random Name Generator PHP

In this Random Name Generator PHP Tutorial you will learn how to generate random names using a handy PHP script. This can be useful for coming up with names for social marketing campaigns, fun or creating mock online accounts.

To execute the below script on a local machine free easyPHP was used.

For this tutorial we will be using a text (*.txt) file as the source of names. The file contains first and last names separated by commas with a line termination after every pair as shown below:

php-random-names-tutorial

The goal of our PHP script would be to generate random combinations of first and last names contained in the source file. To make it a little interesting we will take the number of names a user would like generated as input.

Read source file

Let’s go ahead and read the source file names.txt and store the first and last names in separate arrays.

Two arrays are declared to store the first and last names separately. Next we read the file opened in the $file variable line by line. The explode PHP function stores the first and last names in the $nameArray on the first and second positions respectively. Notice how we tokenized the line based on the comma in the explode function.
Next we have pushed the first and last names into the $firstNameArr and $lastNameArr respectively using the array_push PHP function.
Now that we have read the names from the source file into arrays, we can use programming to output them in a random manner.

HTML part

Before that, let’s write the HTML part of the code that will ask the user for the amount of names they want printed.

echo "<form method='post'>Number of Names: <input type='number' name='numOfNames'><input type='submit' value='Generate Names!'></form>";


The above code will produce something like this in your web browser:

Randomize

To print random combinations of first and last names, we will use simple logic. We will print names at random positions in the $firstNameArr and $lastNameArr arrays.

In the above code we have used the rand PHP function to select names located at random positions of the first and last name arrays. The $pos variable increments with the for loop and further randomizes the choices so the chance of duplicate names being printed remains low.

Last but not least, let’s go ahead and close the code.

Finish

fclose($file);
?>

This was a simple example of a random name generator. To spruce things up you can try interchanging first and last names, or by combining two different names to form a single name!

Example

random name generator php

In order to get the full script for this Random Name Generator PHP Tutorial just email me. But only if you can't get it to work. But all you need is on this page already. 🙂 Sorry, I no longer have it.

[easyazon_image align=”center” height=”500″ identifier=”1449392776″ locale=”UK” src=”https://www.simonmuckle.com/wp-content/uploads/2018/02/51RMQ1jIcPL.jpg” tag=”persowebsi0c-21″ width=”384″]

[easyazon_cta align=”center” identifier=”1449392776″ key=”wide-orange” locale=”UK” tag=”persowebsi0c-21″]

[easyazon_link keywords=”Microsoft Windows 10 Key” locale=”UK” tag=”persowebsi0c-21″]Get Microsoft Windows 10 Key from Amazona[/easyazon_link]

[easyazon_link keywords=”Microsoft Windows 7 key” locale=”UK” tag=”persowebsi0c-21″]Get Microsoft Windows 7 key from Amazona[/easyazon_link]