PHP Notice: Undefined index although using try\catch

php, try-catch

Solution

use `isset` function to check if the variable is set or not:

if( isset($_GET['id'])){
    $api = new api($_GET["id"]);
    echo $api -> processRequest();
}

Problem

This is my try/catch block in PHP: ``` try { $api = new api($_GET["id"]); echo $api -> processRequest(); } catch (Exception $e) { $error = array("error" => $e->getMessage()); echo json_encode($error); } ``` When there is nothing in the `$_GET["id"]`, I still get the notice error. How can I avoid getting this error?

Original source