PHP函数没有返回值

时间:2013-08-05 07:44:32

标签: php function return

由于某种原因,我无法让我的函数返回一个字符串......

$password = crypt_password_input($password, "");

//Encrypt Password longer than 8 characters
function crypt_password_input($inputPassword, $newPassword)
{
    $passwordLength = strlen($inputPassword);

    if($passwordLength > 8){
        $encryptString = substr($inputPassword, 0, 8);
        $inputPassword = substr($inputPassword, 8);
        $newPassword .= crypt($encryptString, "HIDDENSALT");
        crypt_password_input($inputPassword, $newPassword);
    }else{
        $newPassword .= crypt($inputPassword, "HIDDENSALT");
        echo "Final: " . $newPassword . "<br/>";
        return $newPassword;
    }
}


echo "Encrypted from the input: " . $password . "<br/>";

这是此脚本的输出......

决赛:ltu1GUwy71wHkltVbYX1aNLfLYltEZ7Ww8GghfM
从输入加密:

2 个答案:

答案 0 :(得分:3)

在此条件块下您没有return语句。我在那里增加了回报。

if($passwordLength > 8)
{
    $encryptString = substr($inputPassword, 0, 8);
    $inputPassword = substr($inputPassword, 8);
    $newPassword .= crypt($encryptString, "HIDDENSALT");
    return crypt_password_input($inputPassword, $newPassword);
}

答案 1 :(得分:0)

我不确定你的逻辑,但你的代码应该是这样的:

$password = crypt_password_input($password, "");

//Encrypt Password longer than 8 characters
function crypt_password_input($inputPassword, $newPassword)
{
    $passwordLength = strlen($inputPassword);

    if($passwordLength > 8)
    {
        $encryptString = substr($inputPassword, 0, 8);
        $inputPassword = substr($inputPassword, 8);
        $newPassword .= crypt($encryptString, "HIDDENSALT");
        return crypt_password_input($inputPassword, $newPassword);
    }
    else
    {
        $newPassword .= crypt($inputPassword, "HIDDENSALT");
        echo "Final: " . $newPassword . "<br/>";
        return $newPassword;
    }
}


echo "Encrypted from the input: " . $password . "<br/>";

在您的代码中,您递归调用输入但未返回任何内容,因此如果密码长度超过8个字符,则会失败。