facebook pixel
go top


  • Colection of 65 PHP scripts for $4.29 each

Generate a random password with PHP

Limited offer!

Get a huge collection of PHP scripts in a special bundle!

Mega Sale Price
In this tutorial, you will find out how to generate random passwords with a handy PHP function.

It's always better to use a randomly generated password rather than your name, birthday, city, etc. Nowadays most registration forms require you to input a secure password and show you a warning message if the password is too simple. If you are creating a registration system for your PHP project, it will be useful to suggest a password to people who register. Using PHP, it's pretty easy to generate a random password.

Using the function below you can specify what kind of symbols your password(s) should contain, what the password length should be, and how many passwords you want to generate. The output will be an array with generated password(s).


function randomPassword($length,$count, $characters) {

// $length - the length of the generated password
// $count - number of passwords to be generated
// $characters - types of characters to be used in the password

// define variables used within the function
$symbols = array();
$passwords = array();
$used_symbols = '';
$pass = '';

// an array of different character types
$symbols["lower_case"] = 'abcdefghijklmnopqrstuvwxyz';
$symbols["upper_case"] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$symbols["numbers"] = '1234567890';
$symbols["special_symbols"] = '!?~@#-_+<>[]{}';

$characters = split(",",$characters); // get characters types to be used for the passsword
foreach ($characters as $key=>$value) {
$used_symbols .= $symbols[$value]; // build a string with all characters
$symbols_length = strlen($used_symbols) - 1; //strlen starts from 0 so to get number of characters deduct 1

for ($p = 0; $p < $count; $p++) {
$pass = '';
for ($i = 0; $i < $length; $i++) {
$n = rand(0, $symbols_length); // get a random character from the string with all characters
$pass .= $used_symbols[$n]; // add the character to the password string
$passwords[] = $pass;

return $passwords; // return the generated password

$my_passwords = randomPassword(10,1,"lower_case,upper_case,numbers,special_symbols");



Here are a few examples how to generate different random passwords using PHP

// generate one password using 5 upper and lower case characters

// generate three passwords using 10 lower case characters and numbers

// generate five passwords using 12 lower case and upper case characters, numbers and special symbols

Hope this tutorial was useful for you! Good luck with your projects!

Special Offer!

Get all 65 PHP scripts in a bundle for $4.29 each only!

Mega Sale Offer

7 Comments to "Generate a random password with PHP"

  • Moisés


    December 24, 2020 at 15:01 pm

    Guys, just chance the split() function – that was removed from PHP 7 – for preg_split(). Do not forgot to put the partern inside a delimiter (/) like preg_split("/,/",$characters);
    I hope you enjoy. I really love it. Thanks guys.

  • ROhan


    January 15, 2019 at 20:25 pm

    The function is deprecated in PHP 5.3 and removed in PHP 7. This code will no longer work...

    • Joshua


      May 19, 2020 at 03:48 am

      Which function was deprecated? I take it you obviously don't mean the user-defined function.

  • Rahul


    October 25, 2018 at 09:13 am

    This code not working in Linux Hosting on Godaddy. Plz Help

  • darek44


    August 6, 2018 at 13:53 pm

    See my implementations. It uses world all signs.

    • Jolhn


      November 25, 2019 at 01:14 am

      Your website "website no longer working" lol is this an example of your implementations?
      Onya champ

  • Pr0grammer


    March 17, 2018 at 01:15 am

    Funktioniert leider mit PHP 7+ nicht mehr. :-(

    • ProgFish


      July 16, 2018 at 21:52 pm

      To make it work with PHP 7+, replace in line 21
      $characters = split(",", $characters);
      $characters = explode(",", $characters);

Add your comment

    • Free Scripts

      Add great new functionalities to your website with our Free Scripts collection.

      Free scripts
    • PHP Scripts

      Check our extensive collection of top-notch PHP Scripts that will enhance your website!

      Commercial PHP scripts