Error Reporting
suggest changeIf display_errors is on, PHP writes errors, warnings, and notices to the output when a script hits a reportable condition. That helps debugging, and it also exposes paths and other details you do not want users to see.
Therefore it’s good practice to avoid displaying those messages which will reveal information about your server, like your directory tree for example, in production environments. In a development or testing environment these messages may still be useful to display for debugging purposes.
A quick solution
You can turn them off so the messages don’t show at all, however this makes debugging your script harder.
<?php
ini_set("display_errors", "0");
?>
Or change them directly in the php.ini.
display_errors = 0
Handling errors
A better option would be to store those error messages to a place they are more useful, like a database:
set_error_handler(function($errno , $errstr, $errfile, $errline){
try{
$pdo = new PDO("mysql:host=hostname;dbname=databasename", 'dbuser', 'dbpwd', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
if($stmt = $pdo->prepare("INSERT INTO `errors` (no,msg,file,line) VALUES (?,?,?,?)")){
if(!$stmt->execute([$errno, $errstr, $errfile, $errline])){
throw new Exception('Unable to execute query');
}
} else {
throw new Exception('Unable to prepare query');
}
} catch (Exception $e){
error_log('Exception: ' . $e->getMessage() . PHP_EOL . "$errfile:$errline:$errno | $errstr");
}
});
This handler logs the message to the database, or to the error log if that insert fails, instead of printing it. set_error_handler() does not receive fatals such as E_ERROR, compile errors, or uncaught exceptions. Those still need display_errors, log_errors, or an exception handler.