Skip to content

Instantly share code, notes, and snippets.

@guimadaleno
Created September 1, 2022 15:29
Show Gist options
  • Save guimadaleno/8ec01944ee552dcd6d993316ad4cae5d to your computer and use it in GitHub Desktop.
Save guimadaleno/8ec01944ee552dcd6d993316ad4cae5d to your computer and use it in GitHub Desktop.
Generate random password
<?php
/**
* Generate a random password
* @param int $length
*/
function random_password ($length = 8, $chars = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789")
{
$pw = [];
$l = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++):
$n = rand(0, $l);
$pw[] = $chars[$n];
endfor;
return implode($pw);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment