In php, there are many ways to generate unique alphanumeric id. First of all, we will understand why we might need unique alphanumeric id in first place.

In applications, normally we use unique integer ids which is generally generated by database itself ~ Auto-increment, unique integers. But sometimes we need unique alphanumeric string as ids instead of simple integers.

Few Reasons:

  • Unique ID is needed if you have multiple-server app architecture i.e - Your database is stored in multiple servers
  • If you want to merge different branches of data without clashes
  • Unique ID is helpful and must if you want to prevent sequence discovery of data in application or API endpoints.

Generate Unique Alphanumeric String using uniqid() function in PHP

In PHP, there is a very helpful function uniqid() which generates unique alphanumeric string for you. The function has two parameters ~ though both are optional -

#php

PHP: How to generate a Unique id in PHP (Alphanumeric String)
1.50 GEEK