Category Archives: How to generate unique id in php?

How to generate unique ID in php

Many developers use md5 or rand() function to generate the unique id in php, Which is actually not a good practice under some senario. The best technique is to use the pre given php function whic is uniqid()

Example:

 

    // generate unique string
    echo uniqid();
    /* prints
    4bd67c947233e
    */  

    // generate another unique string
    echo uniqid();
    /* prints
    4bd67c9472340
    */
    // with prefix
    echo uniqid('ilovephp_');
    /* prints
    ilovephp_4bd67d6cd8b8f
    */  

    // with more entropy
    echo uniqid('',true);
    /* prints
    4bd67d6cd8b926.12135106
    */  

    // both
    echo uniqid('phpfresher_',true);
    /* prints
    phpfresher_4bd67da367b650.43684647
    */

Related Links for PHP

Link one

Link Two

Link Three