generate unique id

    0

    2

    jvit
    JVIT PHP

    The generateRandomString() function uses either PHP 7's random_bytes() function or openssl_random_pseudo_bytes() function to generate a 16-byte random string. The bin2hex() function converts this random string into a hexadecimal string.

    Shortcut: str.unique

    
    /**
    * Generate random string
    * @param int $length
    * @return string
    */
    function generateRandomString($length = 16)
    {
      // PHP 7
      if (function_exists('random_bytes')) {
        $bytes = random_bytes($length / 2);
      } else {
        $bytes = openssl_random_pseudo_bytes($length / 2);
      }
      return bin2hex($bytes);
    }
    
    /**
    * Simple function to generate time-base unique id
    * If you need more feature, please use: https://uuid.ramsey.dev
    * @param string $prefix the prefix of result
    *
    * @return string
    */
    function generateUniqueID($prefix = null)
    {
      if (!$prefix) {
        $prefix = generateRandomString(8);
      }
      return uniqid($prefix, true);
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.