Skip to main content

PHP Code Challenge #1

 The task is to create a simple program that generates a random password based on certain criteria. Here are the requirements:

<?php

function generatePassword() {

    $length = 20;

    $uppercaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    $lowercaseLetters = 'abcdefghijklmnopqrstuvwxyz';

    $numbers = '0123456789';

    $specialCharacters = '!@#$%^&*()_-+=';

    

    $allCharacters = $uppercaseLetters . $lowercaseLetters . $numbers . $specialCharacters;

    

    $password = '';

    

    for ($i = 0; $i < $length; $i++) {

        $randomIndex = mt_rand(0, strlen($allCharacters) - 1);

        $password .= $allCharacters[$randomIndex];

    }

    

    return $password;

}


// Example usage:

$password = generatePassword();

echo "Generated Password: $password\n";


?>


Comments