php - Random alphanumeric pin? -


I am working on creating a random 5 character pin for a script. I was thinking that someone else would be better?

  $ pin = ''; While ($ i! = 5) {$ string = '23456789abcdefghjkmnprstvwxyzABCDEFGHJKLMNPRSTVWXYZ'; $ Pos = rand (0, (strlen ($ string) -1); $ Pin = $ Pin $ string [$ status]; $ I ++;} echo $ pin;    

just slightly different / smaller valid characters is no reason to declare each time, and is considered superior to the often time loop is.

  $ pinlength = 5; $ CharSet = '23456789abcdefghjkmnprstvwxyzABCDEFGHJKLMNPRSTVWXYZ'; $ pin = ''; ($ a = 0; $ a & lt; $ pinlength; $ a ++) $ pin. = $ CharSet [rand (0, Streln ($ charSet))]; echo $ pin;   

or just for the hell:

  $ MakeCharacterSelect = Function ($ CharSet) {return function usage ($ charSet) {return $ charSet [rand (0, strollon ($ charset));} $ $ pinang = 5; $ C = $ makeCharacterSelect ('23456789abcdefghjkmnprstvwxyzABCDEFGHJKLMNPRSTVWXYZ'); $ pin = ''; ($ A = 0; $ a & lt; $ Pinlanta; $ a ++) $ Pin = $ c ();    

Comments