Skip to content

Instantly share code, notes, and snippets.

@raphaelchaib
Created September 2, 2013 17:26
Show Gist options
  • Select an option

  • Save raphaelchaib/6415268 to your computer and use it in GitHub Desktop.

Select an option

Save raphaelchaib/6415268 to your computer and use it in GitHub Desktop.
PHP: Simple random password generator
<?php
function randomPassword($length = 10) {
$alphabet = 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < $length; $i++) {
$n = rand(0, $alphaLength);
$pass = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment