Make Code

Very simple handy little function to generate a random code of number and lower and upper case letters.

Straight forward stuff…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function makecode($chars = 6)
{
	for($i=0; $i<=($chars-1); $i++)
	{
		$r0 = rand(0,1);
		$r1 = rand(0,2); // frequency of lower case
		if($r0==0)
			$r .= chr(rand(ord('A'),ord('Z')));
		elseif($r0==1)
			$r .= rand(0,9);
		if($r1==0)
			$r = strtolower($r);
	}
	return $r;
}

$r0 controls if the next digit is a number or a letter
$1 controls if teh letter will be upper or lower. Set it to 0 if you want it to always be lower or change the range to what ever suits you.
Also a number maybe passed to the function to change how many digits in the code to make.

Leave a Reply