What is the best way to create a large number of unique promo codes?

I am trying to create promo codes in large batches (with php / mysql).

My code currently looks something like this:

$CurrentCodesInMyDB = "asdfasdf,asdfsdfx"; // this is a giant comma delimited string of the current promo codes in the database.
$PromoCodes = "";
for($i=0;$i<=30000;$i++)
{
    $NewCode = GetNewCode($PromoCodes, $CurrentCodesInMyDB );
    $PromoCodes .= $NewCode . ","; //this string gets used to allow them to download a txt file
    //insert $newcode into database here
}

function GetNewCode($CurrentList, $ExistingList)
{
    $NewPromo = GetRandomString();
     if(strpos($CurrentList, $NewPromo) === false && strpos($ExistingList, $NewPromo) === false)
     {
          return $NewPromo; 
     }  
     else
     {
          return GetNewCode($CurrentList, $ExistingList);   
     }
}

function GetRandomString()
{
     return "xc34cv87"; //some random 8 character alphanumeric string
}

When I make parties in 10k, everything seems to be in order. But the client would like to be able to generate 30 thousand at a time. When I hit the loop up to 30k, I have problems. Are there any obvious performance improvements that I could make, or maybe in another way I could do this?

+3
source share
3 answers

, 30 000 . , code ( ) , 30 000 .

+4

?

: CSV, - .

60 000 strpos() ~ 250 -...

+2

( ), in_array . in_array, - , array_flip,

http://www.php.net/manual/en/function.in-array.php#96198

+1
source

Source: https://habr.com/ru/post/1786430/


All Articles