Created
August 19, 2010 16:43
-
-
Save jgv/538323 to your computer and use it in GitHub Desktop.
generate random numbers in php with no repeats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randomDigits($length){ | |
$numbers = range(0,9); | |
shuffle($numbers); | |
for($i = 0;$i < $length;$i++) | |
$digits .= $numbers[$i]; | |
return $digits; | |
} |
just doing like that
function randomDigits($length){ // function with parameter you must define it before callback the function
global $digits; // global to access from anyway
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
echo randomDigits(4); // 4 mean your number between 4 digits
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can just search the value in your database, if it returns null then it has not been stored yet.