PHP Notice: Undefined index although using try\catch
问题 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? 回答1: use isset function to check if the variable is set or not: if( isset($_GET['id'])){ $api = new api($_GET["id"]); echo $api -> processRequest(); } 回答2: If you want a fast and "dirty"