Generate a Random Secure Password with PHP

You may use the following PHP script to generate a random secure password. The random secure password may include lowercase letters, uppercase letters, numbers and special characters.

Generate a Random Secure Password with PHP
Photo by Ben Griffiths / Unsplash

You may use the following PHP script to generate a random secure password. The random secure password may include lowercase letters, uppercase letters, numbers and special characters.

<?php
function generatePassword($length = 15) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{};:,.<>?';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        $password .= $chars[rand(0, strlen($chars) - 1)];
    }
    return $password;
}

$password = generatePassword();
echo $password;
?>

By default the length of the password is 15 characters. You may change the length of the password on line 2.