create a password with a custom length, uppercase letters and lowercase letters in php

You can use the str_shuffle() function to randomly shuffle a string and substr() function to select a portion of the string with a custom length to generate a password.

main.php
function generatePassword($length) {
  // define the characters that will be used for the password
  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  // shuffle the characters
  $shuffledChars = str_shuffle($chars);
  // select a portion of the shuffled string with the desired length
  $password = substr($shuffledChars, 0, $length);
  // return the password
  return $password;
}

// example usage
$password = generatePassword(8); // generates an 8-character password
echo $password;
504 chars
15 lines

To include uppercase letters, you can modify the characters string to include them:

main.php
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
65 chars
2 lines

To include numbers and special characters, you can add them to the characters string as well.

gistlibby LogSnag