简体   繁体   English

PHP函数不会打印带变量的消息

[英]PHP function won't print message with variable

So I'm trying to create some sort of error messages, code looks like this: 所以我试图创建一些错误消息,代码如下所示:

function callError($errorcode, $attempts) {
    $errormessage = array(
        "0" => "Du har angivit fel lösenord, du har ". $attempts ." försök kvar. Kontrollera att du har skrivit rätt användarnamn.",
        "1" => "Ditt konto har låsts, kontakta webmaster för att återställa det.",
        "2" => "Användare finns inte, kontrollera att du har skrivit rätt användarnamn."
    );

    return $errormessage[$errorcode];
}

but when I run the first error message it doesn't work, it won't even show up. 但是当我运行第一条错误消息时它不起作用,它甚至都不会出现。 On the other hand when I run the other two it works flawless! 另一方面,当我运行另外两个时,它的工作完美无瑕! I've tried to return an array containing $errormessage and $attempts but that doesn't work either. 我试图返回一个包含$ errormessage和$ attempts的数组,但这也不起作用。

What am I doing wrong here? 我在这做错了什么?

It seems to be a problem on type of variables. 这似乎是变量类型的问题。

Make sure you are consistent on this. 确保你在这方面保持一致。 For example, let's decide we want the values to be int : 例如,让我们决定我们希望值为int

function callError($errorcode, $attempts) {

    $errorcode = (int) $errorcode;
    $errormessage = array(
        0 => "Du har angivit fel lösenord, du har ". $attempts ." försök kvar. Kontrollera att du har skrivit rätt användarnamn.",
        1 => "Ditt konto har låsts, kontakta webmaster för att återställa det.",
        2 => "Användare finns inte, kontrollera att du har skrivit rätt användarnamn."
    );

    return $errormessage[$errorcode];
}

Try to use a switch with a fallback safety mechnism. 尝试使用具有后备安全机制的开关。

function callError($errorcode, $attempts) {
    $output = '';

    switch((int)$errorcode) {
        case 0:
            $output = 'Du har angivit fel lösenord, du har '. $attempts .' försök kvar. Kontrollera att du har skrivit rätt användarnamn.';
            break;
        case 1:
            $output = 'Ditt konto har låsts, kontakta webmaster för att återställa det.';
            break;
        case 2:
            $output = 'Användare finns inte, kontrollera att du har skrivit rätt användarnamn.';
            break;
        default:
            $output = 'SOMETHING WENT WRONG [CUSTOM MESSAGE]'.$errorcode.' : '.$attempts;
            break;
    }

    // Do something more here with your error handling if needed

    // Return the output message
    return $output;
}

This will force typecast $errorcode to an Integer. 这将强制将类型转换$errorcode转换$errorcode Integer。 But it doesn't really matter for the default case, yet it will rule out problems with integers inside a string "1" . 但它对于default情况并不重要,但它将排除字符串"1"内的整数问题。

Make sure index is String Add 确保index是String Add

$errorcode = "$errorcode";

as the first line in the func 作为func中的第一行

Tip: change that to a switch() statement: 提示:将其更改为switch()语句:

function callError($errorcode, $attempts) {
    switch ($errorcode) {
        case 0:
            return "Du har angivit fel lösenord, du har ". $attempts ." försök kvar. Kontrollera att du har skrivit rätt användarnamn.";
        case 1:
            return "Ditt konto har låsts, kontakta webmaster för att återställa det.";
        case 2:
            return "Användare finns inte, kontrollera att du har skrivit rätt användarnamn.";
        default:
            return "No error associated";
    }
}

The answer was just to remove the numbers that were stringed in the array. 答案只是删除数组中的数字。

See @fedorqui comment! 见@fedorqui评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM