{"id":1308,"date":"2018-01-19T19:22:41","date_gmt":"2018-01-19T19:22:41","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/error-handling-in-php-5-page-2\/"},"modified":"2018-01-19T19:24:41","modified_gmt":"2018-01-19T19:24:41","slug":"error-handling-in-php-5-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/error-handling-in-php-5-page-2\/","title":{"rendered":"Error Handling in PHP 5 Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By J. Leidago Noabeb<\/div>\n<div class=\"\">on April 16, 2012<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>PHP Error Handling on Production Sites<\/h2>\n<p>For those of you who are running production sites, switch off <em>display_errors<\/em>. Although these errors have become somewhat safer in terms of the content that it displays, they still pose a security risk to your site. To manage errors that occur during production, make use of PHP&#8217;s error-handling capabilities. To demonstrate these capabilities, we will create a custom error handler. Before we start developing our own error handler, I&#8217;d like to share the following things about PHP&#8217;s error reporting. PHP has three kinds of error levels:<\/p>\n<ul>\n<li>Warnings, which indicate a problem but don&#8217;t stop a script&#8217;s execution.<\/li>\n<li>Errors, which stop a script from continuing (including the ever-common parse error, which prevent scripts from running at all).<\/li>\n<li>Notices, which do not stop the execution of a script and may not necessarily be a problem.<\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<p>This will sound familiar to you, since we already looked at each type in detail. The three error types each have reporting levels as listed below:<\/p>\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"127\">\n<h3>Number<\/h3>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<h3>Constant<\/h3>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<h3>Reporting on<\/h3>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_ERROR<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>Fatal runtime errors (that stop execution of the script)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_WARNING<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>Runtime warnings (non-fatal errors)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_PARSE<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>Parse errors<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>8<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_NOTICE<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>Notices (things that may or may not be a problem)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>256<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_USER_ERROR<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>User-generated error messages, generated by the trigger_error() function<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>512<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_USER_WARNING<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>User-generated warnings, generated by the <code>trigger_error()<\/code> function<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>1024<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_USER_NOTICE<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>User-generated notices, generated by the trigger_error() function<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>2048<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_STRICT<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>Recommendations for compatibility and interoperability(available as from PHP6)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"127\">\n<p>8191<\/p>\n<\/td>\n<td valign=\"top\" width=\"156\">\n<p>E_ALL<\/p>\n<\/td>\n<td valign=\"top\" width=\"355\">\n<p>All errors, warnings, and recommendations<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In our custom error handler, we will try to catch the type of error that PHP is reporting and then handle it appropriately. We can either send the error to an email address, or we can log the error to a file. Our error handler will be a class, which will get its information from a config file:<\/p>\n<h3>Config file:<\/h3>\n<div id=\"code\">\n<pre class=\"brush: js;\">&lt;?php\n$app_name=\"Error Manager\";\n$filename = 'error_log.txt'; \/\/filename to which the error will be logged\n$email ='admin@website.com';\/\/email address to which error messages are to be sent to\n$emailname = 'joe blogg'; \/\/email recipient name\n$currdate=date(\"l, dS of F Y @ H:i:s A\");\n\n?&gt;<\/pre>\n<\/div>\n<p>The config file contains the email address, logfile and current date information. We can easily include this information in the main class, but it is better security-wise to have it somewhere (even outside of the root) where you can make changes.<\/p>\n<h3>Errorhandler class:<\/h3>\n<div id=\"code\">\n<pre class=\"brush: js;\">&lt;?php\n\/*\nfunctions:\nhandle error - Actual error handler\nvars are not echoed when the error is printed out. You can do this if you so wish\nTO DO:\nHTML Format error messages\n*\/\nclass errors\n{\n\n\u00a0\u00a0\u00a0 var $msg;\n\u00a0\u00a0\u00a0 function errors()\n\u00a0\u00a0\u00a0 {\n\u00a0\u00a0\u00a0 global $filename,$email,$currdate,$app_name;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Populate class variables\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;email = $email;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;logfile =$filename;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;currdate=$currdate;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;appname=\"Error Manager\";\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Take over from PHP error handling\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 set_error_handler(array($this, 'handle_error'));\n\u00a0\u00a0\u00a0 }\n\n\n\u00a0\u00a0\u00a0 function handle_error($type, $string, $efile, $line, $vars)\n\u00a0\u00a0\u00a0 {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Decide which type of error it is, and handle appropriately\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch ($type)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Error type\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_ERROR:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_USER_ERROR:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/build the message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg = '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line.'&lt;\/b&gt; &lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/$this-&gt;msg .= print_r($vars);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(isset($this-&gt;appname)){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated by &lt;b&gt;'.$this-&gt;appname.'&lt;\/b&gt; on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }else{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*\/\/log the error to file\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,3,$this-&gt;logfile);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/sent message to web administrator\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,1,$this-&gt;email); *\/\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/print out message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line. '&lt;\/b&gt;&lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo 'Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\n\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_WARNING:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_USER_WARNING:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/build the message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg = '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line.'&lt;\/b&gt; &lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ $this-&gt;msg .= print_r($vars);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(isset($this-&gt;appname)){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated by &lt;b&gt;'.$this-&gt;appname.'&lt;\/b&gt; on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }else{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*\/\/log the error to file\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,3,$this-&gt;logfile);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/sent message to web administrator\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,1,$this-&gt;email); *\/\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/print out message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line. '&lt;\/b&gt;&lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo 'Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_PARSE:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/build the message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg = '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line.'&lt;\/b&gt; &lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u00a0 $this-&gt;msg .= print_r($vars);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(isset($this-&gt;appname)){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated by &lt;b&gt;'.$this-&gt;appname.'&lt;\/b&gt; on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }else{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*\/\/log the error to file\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,3,$this-&gt;logfile);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/sent message to web administrator\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,1,$this-&gt;email); *\/\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/print out message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line. '&lt;\/b&gt;&lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo 'Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_NOTICE:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_USER_NOTICE:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/build the message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg = '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line.'&lt;\/b&gt; &lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ $this-&gt;msg .= print_r($vars);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(isset($this-&gt;appname)){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated by &lt;b&gt;'.$this-&gt;appname.'&lt;\/b&gt; on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }else{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*\/\/log the error to file\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,3,$this-&gt;logfile);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/sent message to web administrator\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,1,$this-&gt;email); *\/\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/print out message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line. '&lt;\/b&gt;&lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo 'Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\n\u00a0\u00a0\u00a0 }\n\n\u00a0\u00a0\u00a0 }\n\n\n\n?&gt;<\/pre>\n<\/div>\n<p>The errorhandler class uses a function called <code>handle_error()<\/code> to list all the available error reporting levels and constructs a message using the reported error:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">function handle_error($type, $string, $efile, $line, $vars)\n\u00a0\u00a0\u00a0 {\u00a0\n\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Decide which type of error it is, and handle appropriately\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch ($type)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Error type\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_ERROR:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case E_USER_ERROR:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/build the message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg = '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line.'&lt;\/b&gt; &lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/$this-&gt;msg .= print_r($vars);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(isset($this-&gt;appname)){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated by &lt;b&gt;'.$this-&gt;appname.'&lt;\/b&gt; on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }else{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;msg .='Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';<\/pre>\n<\/div>\n<p>Then it writes the error message to a file:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*\/\/log the error to file\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,3,$this-&gt;logfile);<\/pre>\n<\/div>\n<p>And then sends the error to the email address that was defined in the config file:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/sent message to web administrator\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log($this-&gt;msg,1,$this-&gt;email); *\/\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/print out message\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;b&gt;Error:&lt;\/b&gt; &lt;br&gt; Error message: &lt;b&gt;'.$string.'&lt;\/b&gt; in &lt;b&gt;'.$efile.'&lt;\/b&gt; on line &lt;b&gt;'. $line. '&lt;\/b&gt;&lt;br&gt;' ;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo 'Error generated on &lt;b&gt;'.$this-&gt;currdate.'&lt;\/b&gt;&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;br&gt;';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/pre>\n<\/div>\n<p>To test the errorhandler class, you simply do the following:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">&lt;?php\ninclude \"errorhandler.class.php\";\ninclude \"config\/errorhandler.conf.php\";\n\/\/instantiate the class\n\u00a0$errors = new errors();\n\/\/trigger some\u00a0 errors\n$row = mysql_fetch_array($result);\n100\/0;\n?&gt;<\/pre>\n<\/div>\n<p>I&#8217;ve included the source code for download.<\/p>\n<h2>Conclusion<\/h2>\n<p>Handling errors this way keeps your site secure and enables you to deal with any errors in the background. The error handler can, of course, be expanded to include any new error reporting levels that might be introduced in the newer versions of PHP.<\/p>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Leidago_Noabeb03062012.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Leidago_Noabeb03062012.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to handle PHP errors in the various error types, an inevitable part of software development.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1308","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1308"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1308\/revisions"}],"predecessor-version":[{"id":3198,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1308\/revisions\/3198"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}