简体   繁体   中英

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.

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 :

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. But it doesn't really matter for the default case, yet it will rule out problems with integers inside a string "1" .

Make sure index is String Add

$errorcode = "$errorcode";

as the first line in the func

Tip: change that to a switch() statement:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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