Itβs easier to use.
Uppercase
$letter = chr(rand(65,90));
Lowercase
$letter = chr(rand(97,122));
ASCII chart
The code below generates a random alphanumeric string of length $. You can see the numbers there that you need.
function izrand($length = 32) { $random_string=""; while(strlen($random_string)<$length && $length > 0) { $randnum = mt_rand(0,61); $random_string .= ($randnum < 10) ? chr($randnum+48) : ($randnum < 36 ? chr($randnum+55) : $randnum+61); } return $random_string; }
update: 12.19.2015
Here is the updated version of the function above, it adds the ability to generate a random digital key OR an alphanumeric key. To generate a number, just add the second parameter as true .
Usage example
$randomNumber = izrand(32, true);//generates 32 digit number as string $randomNumber = izrand(32, true);//generates 32 digit number as string $randomAlphaNumeric = izrand();//generates 32 digit alpha numeric string $randomAlphaNumeric = izrand();//generates 32 digit alpha numeric string
Type cast to Integer
If you want to enter a number into an integer, just do it after generating the number. NOTE. This will remove any leading zeros if they exist.
$randomNumber = (int) $randomNumber;
Isrand () v2
function izrand($length = 32, $numeric = false) { $random_string = ""; while(strlen($random_string)<$length && $length > 0) { if($numeric === false) { $randnum = mt_rand(0,61); $random_string .= ($randnum < 10) ? chr($randnum+48) : ($randnum < 36 ? chr($randnum+55) : chr($randnum+61)); } else { $randnum = mt_rand(0,9); $random_string .= chr($randnum+48); } } return $random_string; }
source share