PHP get random number

<?php
function randomkeys($length)
{
$pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
for($i=0;$i<$length;$i++)
{
$key .= $pattern{mt_rand(0,35)}; //generate php random number
}
return $key;
}
echo randomkeys(4);
?>

<?php
echo(rand();
echo(rand();
echo(rand(10,100))
?>
output:

17757
3794
97
Another way:
function randomkeys($length)
{
$output='';
for ($a = 0; $a < $length; $a++) {
$output .= chr(mt_rand(33, 126)); //Generate php random number
}
return $output;
}
echo randomkeys(8);

In the second php random function, first use mt_rand() to generate a php random number between 33 and 126, and then use the chr() function to convert it into a character. Looking at the ascii code table, you will find that 33 to 126 represent all the characters in the character pool in the first function. The second function does the same thing as the first, but is more concise.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish