Unique Alphanumeric String in PHP

1 6,300

In this article, we will discuss how to create a unique alphanumeric string in PHP. We are creating a PHP function which creates a unique string contains letter and number (alphanumeric) with the character limit. These kinds of unique string can be used as Voucher Code, Referral Code, Coupon Code, OTP or any kind of Promotional Code.

Here’s the PHP function:

function unique_code($limit)
{
  return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
}
echo unique_code(8);

// Output looks
s5s108df
12df54vf

In the above code snippet, I’m using the default PHP functions.

Add custom function in Laravel Helper

You can use this in the Laravel Application. You need to add this function to your Helper.php 

If you are not familiar with the custom helper in Laravel then you can follow this tutorial.

Hope you like this post, Please feel free to add the comment if any query.

Leave A Reply

Your email address will not be published.

1 Comment
  1. Michael says

    You’re cool.