There may be situations where you need to generate a unique string. I have seen many people use the md5() function for this, even though it’s not exactly meant for this purpose:
// generate unique string echo md5(time() . mt_rand(1,1000000));
There is actually a PHP function named uniqid() that is meant to be used for this.
// generate unique string echo uniqid(); /* prints 4bd64c947233e */ // generate another unique string echo uniqid(); /* prints 4bd67c9412340 */
You may notice that even though the strings are unique, they seem similar for the first several characters. This is because the generated string is related to the server time. This actually has a nice side effect, as every new generated id comes later in alphabetical order, so they can be sorted.
Read more »
