Thursday, January 28, 2016

GENERATE RANDOM CODE IN PHP: Create a Random Code From A-Z a-z 0-9

Hi there! Today I created by own version in generating random code in PHP from characters A-Z, a-z and 0-9. You can set how many characters to display by setting it in the variable $limit.


PHP Code
<?php
$string="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//62 is the length of the string. You can check the length of a string using
//strlen($string) function

$limit=10;
$i=0;
$code="";
while($i<$limit)
{
$rand=rand(0,62);
$code=$code.$string[$rand];
$i++;
}
echo $code;
?>

The code above will return 10 random characters from the $string and will generate something like the output below.

Sample Output
fBicU0qbbG / m8I3h7FEjU

GENERATE RANDOM CODE IN PHP: Create a Random Code From A-Z a-z 0-9


GENERATE RANDOM CODE IN PHP: Create a Random Code From A-Z a-z 0-9 Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment