PHP Random - Generate random Numbers and Strings

PHP random numbers and strings are easily generated using the PHP rand function. This function can be made to generate pseudo-random numbers within certain boundaries based on the arguments passed to it. Have a look at our PHP random tutorial below.

Note: We might use the term "random" throughout this tutorial, but kindly note that what the 'rand' function generates is pseudo-random numbers.

PHP rand Function Syntax

The syntax of the rand function in the way we will be using it is as follows:

$random_integer = rand($min, $max);

Where $min and $max are lowest and highest values to return.

PHP Random Number Generator

If we want to generate a random number between 20 and 50, we would do it like this:

<?php
 
$number = rand(20, 50);
echo $number;
 
?>

The result will be a number like:

43

By toying with the parameters, we will be able to generate random numbers in any range we like.

Make PHP generate Random String

We can generate a random string in PHP by using random numbers. Remember that a random string is made up of random characters and that each character has a numeric ASCII value which is associated with it. By generating numbers which represent valid ASCII symbols, and then converting those numbers to characters, we will then have our random string.

Have a look at the following code:

<?php
 
function generateRandomString($length)
{
	$result = '';
 
	for ($i = 0; $i < $length; $i++)
	{
		// the use of 97 and 122 below will be explained
		$num = rand(97, 122);
		$result .= chr($num);
	}
 
	return $result;
}
 
echo generateRandomString(15);
 
?>

The output will be a string like:

pccuygmbsywnpae

We used 97 and 122 as the limits to our rand function to obviously keep the numbers within that range. This range was chosen because the ASCII code for the letter 'a' is 97 and for 'z' it is 122. If we use that range, will we generate a random string consisting of lower-case English letters only.

Feel free to use a range of your choice when generating the random numbers. Bear in mind though, that English letters and printable symbols are in the range 32 to 126 and outside of that range will be gibberish.

So if we used:

<?php
 
function generateRandomString($length)
{
	$result = '';
 
	for ($i = 0; $i < $length; $i++)
	{
		$num = rand(32, 126);
		$result .= chr($num);
	}
 
	return $result;
}
 
echo generateRandomString(15);
 
?>

The output would be a string like:

&ZF=9x>$77C[jaw

Note that the above random string has been delimited where necessary to prevent browser issues. For example, suppose we had a < in our random string; browsers would think it was the start of an HTML tag and complain. This delimiting has nothing to do with the PHP code or the random string generation, only for the proper rendering in your browser.

That is it for this comprehensive guide to random number and random string generation in PHP. We hope you found this PHP tutorial useful.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...