【问题标题】:PHP function not returning valuePHP函数不返回值
【发布时间】:2013-08-05 20:30:16
【问题描述】:

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

$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
从输入加密:

【问题讨论】:

  • 实际上你返回的字符串是ltu1GUwy71wHkltVbYX1aNLfLYltEZ7Ww8GghfM

标签: php function return


【解决方案1】:

您在此条件块下没有return 语句。我已经在那里添加了返回。

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

【讨论】:

    【解决方案2】:

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

    $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 个字符,则会失败。

    【讨论】:

    • 在密码长度达到 8 个或更多字符之前,他不希望它返回。
    • 是的,我确实有 $password = crypt_password_input($password, "");由于某种原因,我没有在这里写它大声笑。我确实看到我需要在真实情况下获得回报。谢谢大家:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2017-01-08
    • 2012-11-27
    • 2011-04-20
    • 1970-01-01
    相关资源
    最近更新 更多