1

I have a PHP script that returns a random password. How do I echo out the password that is generated?

<?php

function generatePassword($length=9, $strength=0) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength & 1) {
        $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength & 2) {
        $vowels .= "AEUY";
    }
    if ($strength & 4) {
        $consonants .= '23456789';
    }
    if ($strength & 8) {
        $consonants .= '@#$%';
    }

    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}

?>
1
  • 3
    do you wrote all that code and you don't know about echo? Commented Feb 20, 2011 at 23:32

2 Answers 2

2

As prodigitalson mentioned, you can directly echo out the return value or assign it to a variable, eg

$password = generatePassword();
Sign up to request clarification or add additional context in comments.

Comments

2

err.. change

return $password;

to

echo $password;

Or.. better yet:

$somevar = generatePassword();
echo $somevar;

3 Comments

Functions that echo are so Wordpress. Also, "Wordpress" is my new vernacular for "bad", hopefully it catches on ;)
Hahaha, I agree on that (function that echo are bad). But, I don't want to start 'religion' wars ;)
@Phil Or you could just use the right function. get_ in front of the function name will return the output instead of echo'ing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.