0

EDIT:

if(array_key_exists($errcode, $Errors)){
    $Data['status'] = -1;
    $Data['err'] = array(
        "err_code" => $errcode,
        "err_str" => $Errors[$errcode]
    );
}

I'm having a harsh time figuring out if a key exists in an array, I've tried using array_key_exists method, but no luck! I've also tried empty($array[$key]) which seems to return the same generic error rather than the specific one.


Calling err(null, 3) will output:

{
    "status": -1,
    "err": {
        "err_code": null,
        "err_str": "Generic error"
    }
}

I've tried using the array_key_exists method which returns a bool but it doesn't seem to work, why is this?

My site should output error 5: Invalid


//Errors ENUM
$Errors = array(
    0 => "Cannot parse <GameID>",
    1 => "Invalid steam session",
    2 => "Invalid <GameID>, non-numeric",
    3 => "SQL Connection refused",
    4 => "SQL Query error",
    5 => "invalid <GameID>"
);

function err($status, $errcode){
    if(isset($errcode)){
        if($Errors[$errcode] != null){
            $Data['status'] = -1;
            $Data['err'] = array(
                "err_code" => $errcode,
                "err_str" => $Errors[$errcode]
            );
        } else {
            $Data['status'] = -1;
            $Data['err'] = array(
                "err_code" => null,
                "err_str" => "Generic error"
            );
        } 
    } else {
        $Data['status'] = $status;
        $Data['err'] = array(
            "err_code" => null,
            "err_str" => null
        );
    }
    echo(json_encode($Data, 128 | 32));
}

1 Answers1

0

The err function doesn't see global $Errors variable. Declare Errors variable as global inside Err function:

function err($status, $errcode){
    global $Errors;
    if(isset($errcode)){
        if($Errors[$errcode] != null){
            $Data['status'] = -1;
            $Data['err'] = array(
                "err_code" => $errcode,
                "err_str" => $Errors[$errcode]
            );
        } else {
            $Data['status'] = -1;
            $Data['err'] = array(
                "err_code" => null,
                "err_str" => "Generic error"
            );
        } 
    } else {
        $Data['status'] = $status;
        $Data['err'] = array(
           "err_code" => null,
           "err_str" => null
        );
   }
   echo(json_encode($Data, 128 | 32));
}
Community
  • 1
  • 1
  • 1
    Why should the OP try this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. Code only answers are particularly not acceptable on Stack Overlfow. – Jay Blanchard May 03 '16 at 18:26
  • This would work, but I'm wondering why by solution won't. –  May 03 '16 at 18:30