PHP

HOW TO CATCH ALL ERRORS IN PHP

(quick examples)

DO NOT SHOW ERROR, SAVE TO LOG FILE error_reporting(E_ALL & ~E_NOTICE); ini_set("display_errors", 0); ini_set("log_errors", 1); ini_set("error_log", "error.log");

CUSTOM ERROR SCREEN

01

CATCH ALL - SHOW CUSTOM MESSAGE set_exception_handler(function($ex) { ?> <div style="position:fixed; top:0; left:0;  z-index:9999; width:100vw; height:100vw; background:#fff">   <h1>AN ERROR HAS OCCURED.</h1> </div> <?php });

ERRORS TABLE date         | DATETIME file           | VARCHAR line          | VARCHAR message | TEXT trace       | TEXT

SAVE ERROR TO DATABASE (A)

02

SAVE ERROR TO DATABASE (B)

03

DO NOT SHOW & LOG ERRORS error_reporting(E_ALL & ~E_NOTICE); ini_set("display_errors", 0); ini_set("log_errors", 0);

SAVE ERROR TO DATABASE (C)

04

CATCH ALL - SAVE ERROR TO DATABASE set_exception_handler(function($ex) {   $pdo = new PDO("mysql:host=HOST;   dbname=NAME;charset=utf8mb4",    "USER", "PASSWORD");   $stmt = $pdo->prepare("INSERT INTO    `errors` (`file`, `line`, `message`,  `trace`)    VALUES (?,?,?,?)");     $stmt->execute([$ex->getFile(),   $ex->getLine(),$ex->getMessage(),   $ex->getTraceAsString()]); });