How to make your server never thrown 500

PHP common errors:

1, call the function parameter error

 

array_merge([1,2,3],''); -- Warning: array_merge(): Expected parameter 2 to be an array, string given

 

2, the class does not exist

Fatal error: Uncaught Error: Class 'test' not found

3, the variable is not defined

PHP Notice:  Undefined variable

4, no match an array subscript

PHP Notice:  Undefined index: abc 

5, database storage error

column not found
data too long for name
Invalid datetime format: 1292 Incorrect datetime value: '111' for column 'update_time' at row 1

 

Methods commonly used to trap the error:

1, try catch capture database error

try {
    $TalentModelObj->update($talentInfo,array(['id','=',$talentId]));
    return array('retcode'=>StatusCode::$SUCCESS,'data'=>$talentId,'msg'=>'编辑候选人信息成功');
} catch (\Throwable $th) {
    return array('retcode'=>StatusCode::$ERROR,'data'=>$th,'msg'=> ' Edit failed, an error message: ' .json_encode (TH $)); 
}

2, registration error callback function to trap errors

// set all given level to 
the error_reporting (E_ALL);
 // the set_error_handler - set process user defined 
the set_error_handler ([__ class__, ' AppError ' ]);
 // the set_exception_handler - set user-defined exception handler 
the set_exception_handler ( [__CLASS__, ' appException ' ]);
 // register_shutdown_function - a registration function will be executed when the suspension php, script execution is complete or exit () after the call 
register_shutdown_function ([__ class__, ' appShutdown ' ]);

 

Guess you like

Origin www.cnblogs.com/xuweiqiang/p/11758097.html