Password Generator for PHP

Free online password generator with PHP code examples

Working with password generator in PHP? Our free online password generator helps PHP developers format, validate, and process data instantly. Below you will find PHP code examples using random_int (built-in) so you can achieve the same result programmatically in your own projects.

Try the Password Generator Online

Use our free Password Generator directly in your browser — no setup required.

Open Password Generator

PHP Code Example

<?php
function generatePassword(int $length = 20): string {
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        $password .= $chars[random_int(0, strlen($chars) - 1)];
    }
    return $password;
}

echo generatePassword(20);

Quick Setup

Library: random_int (built-in)
// Built-in function — no installation needed

PHP Tips & Best Practices

  • random_int() is cryptographically secure (PHP 7+)
  • Never use rand() or mt_rand() for password generation
  • Use random_bytes() for raw secure random bytes

Frequently Asked Questions

Password Generator in Other Languages

More PHP Tools