PHP – generate random code

The following php function code will generate random code , you have to call the function name with length for the code 🙂

function generateRandomCode($length)
$chars = "234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
$url = "";
while ($i <= $length) {
$url .= $chars{mt_rand(0,strlen($chars))};
return $url;

and to view the out put just call the function like this

echo generateRandomCode(16);

that’s it.


Published by


Hello, I am a software developer from Bangladesh. I develop professional web sites/applications using LAMP(Linux, Apache, MySql, PHP) Stack. I have 7 years of professional working experience developing websites on WordPress, Magento, Laravel, Yii, Code Igniter. I have in depth knowledge on following language and tools, PHP, C, C++, Java, MySql, HTML(5), CSS(3), JavaScript, jQuery, Linux, Windows, Apache, Ngnix.

2 thoughts on “PHP – generate random code”

  1. Hi Samrat, I am using your file uploader, but the attachments go to my directory folder in /wp-content/uploads instead of sending to an email to me directly. Is there a way to drop a notification to me directly instead of me manually going into the directory folder to check? Thanks. FN

    1. hi,

      thanks to for try it, but it should send the uploaded file to the admin email address as attachment.

      and this plugins takes the email address from wordpress admin -> setting -> admin email . please check the email address is set properly.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s