Location: PHPKode > scripts > Generate Number Code > generate-number-code/generateNumbreCode.class.php
<?

//	Created By: 
//	 Diego Szwebel - hide@address.com
//	 Montevideo - Uruguay

// It generates a random number of $digits digits. The second and third parameter is optional. 
// The second one is used when you want that the code be a string of $digits digits. 
// For that you add zeros in the away numbers. The third parameter says if the minimum number to use the random function begins in 1 or 0.

class generateNumbreCode
{
	function create_random_number($digits_quantity, $string = false, $zero = 1)
	{
		$random_number = 0;
		$digits = 0;
	
		while($digits < $digits_quantity)
		{
			$rand_max .= "9";
			$digits++;
		}
		
		mt_srand((double) microtime() * 1000000); 
		$random_number = mt_rand($zero, intval($rand_max));
	
		if($string)
		{
			if(strlen(strval($random_number)) < $digits_quantity)
			{
				$zeros_quantity = $digits_quantity - strlen(strval($random_number));
				$digits = 0;
				while($digits < $zeros_quantity)
				{
					$str_zeros .= "0";
					$digits++;
				}
				$random_number = $str_zeros . $random_number;
			}
		}
		return $random_number;
	}
}

?>
Return current item: Generate Number Code