Generate random password in PHP


When creating web apps, there’s often a need to generate a random password for your users. It uses PHP’s handy str_shuffle() function:


function random_password( $length = 8 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr( str_shuffle( $chars ), 0, $length );
    return $password;
}

$password = random_password(8);

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.