0

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?

6
  • 2
    What about making it associative but without numbers as strings? Meaning 0 => array(), 1 => array() ... Commented Mar 6, 2013 at 22:52
  • Get rid of the numbers like @fedorqui said and show us how you're calling this function Commented Mar 6, 2013 at 22:56
  • You probably get an $errorcode into the callError() function that isn't 0, 1 or 2 or it fails to comply to the typecast from an integer to a string. It's much better to use try-catch-throw mechanisms :) Commented Mar 6, 2013 at 22:56
  • var_dump($errorcode)....... Commented Mar 6, 2013 at 22:56
  • @fedorqui That was the solution, thanks a bunch! I think I'm getting pretty tired.. Commented Mar 6, 2013 at 23:01

5 Answers 5

1

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];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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".

Comments

0

Make sure index is String Add

$errorcode = "$errorcode";

as the first line in the func

3 Comments

how about just making sure the numbers arnt strings as that is the easier option
This is pretty ancient PHP. And is not a safe solution for his problem sadly. It's better if he handles switch cases and has a default to fall back to.
@Allendar, I agree, was just providing a solution. Switch sounds like the better way of going what Jake wants.
0

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

Comments

0

The answer was just to remove the numbers that were stringed in the array.

See @fedorqui comment!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.