PHP - 赋值函数返回变量

时间:2011-02-20 23:28:21

标签: php

我有一个返回随机密码的PHP脚本。如何回显生成的密码?

<?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;
}

?>

2 个答案:

答案 0 :(得分:3)

正如prodigitals所提到的,您可以直接echo输出返回值或将其分配给变量,例如

$password = generatePassword();

答案 1 :(得分:2)

错误..改变

return $password;

echo $password;

或者......更好的是:

$somevar = generatePassword();
echo $somevar;