How to generate a random, unique, alphanumeric string with PHP?

Spread the love

Sometimes, we want to generate a random, unique, alphanumeric string with PHP.

In this article, we’ll look at how to generate a random, unique, alphanumeric string with PHP.

How to generate a random, unique, alphanumeric string with PHP?

To generate a random, unique, alphanumeric string with PHP, we can use the random_bytes function.

For instance, we write

$bytes = random_bytes(20);
var_dump(bin2hex($bytes));

to cann random_bytes to create a byte strinf with tge given length.

Then we call bin2hex with $bytes to convert the byte string to a hex string.

Conclusion

To generate a random, unique, alphanumeric string with PHP, we can use the random_bytes function.

Leave a Reply

Your email address will not be published.