You are here: Home » PHP » Generate random alphanumeric value in php

Generate random alphanumeric value in php

46 Flares 46 Flares ×

Generating random alphanumeric value in PHP is very simple. See the code below, it will generate random values.

function randompassword($length = 6) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}
$user_pass= randompassword();

$length denotes string length.
If you want only numeric use $characters = ‘0123456789’;
Only want alpha value use $characters=’abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
May change $characters value according to requirement.

About Abhishek Sinha

I am Abhishek and I love to code on PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Scroll To Top
46 Flares Twitter 9 Facebook 9 Google+ 5 StumbleUpon 18 Pin It Share 0 LinkedIn 5 46 Flares ×