<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Albert Fama &#187; Error Handling</title>
	<atom:link href="http://albertfama.com/category/php/error-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://albertfama.com</link>
	<description>Freelance Web Programmer - specializing in PHP &#38; MySQL</description>
	<lastBuildDate>Fri, 20 Nov 2009 16:06:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Display Errors</title>
		<link>http://albertfama.com/php/display-errors/</link>
		<comments>http://albertfama.com/php/display-errors/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 16:06:31 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[Content]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://albertfama.com/?p=975</guid>
		<description><![CDATA[The display_errors directive in the PHP ini file &#8216;tells&#8217; PHP if errors should be added to the output stream (normally printed to the screen) or if they should not be displayed in the browser.
&#160;
&#60;IMPORTANT&#62;
It should be noted that this setting DOES NOT suppress the error. Even if the directive is set to &#8216;Off&#8217; (do not [...]]]></description>
			<content:encoded><![CDATA[<p>The display_errors directive in the PHP ini file &#8216;tells&#8217; PHP if errors should be added to the output stream (normally printed to the screen) or if they should not be displayed in the browser.<br />
<br />&nbsp;<br />
<strong>&lt;IMPORTANT&gt;</strong></p>
<p>It should be noted that this setting <strong>DOES NOT</strong> suppress the error. Even if the directive is set to &#8216;Off&#8217; (do not display errors) errors which occur in a script are still triggered and will be processed by the error handler.</p>
<p><strong>&lt;/IMPORTANT&gt;</strong><br />
<br />&nbsp;<br />
PHP does not offer a special display_errors function, but by using the <a href="http://us.php.net/manual/en/function.ini-set.php"  title="PHP Manual: ini_set()">ini_set()</a> function we can control the setting of this directive within our script. The function signature looks like this:</p>
<p>string ini_set  ( string $varname  , string $newvalue  )</p>
<p>blah, blah</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=975">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/display-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error Reporting</title>
		<link>http://albertfama.com/php/error-reporting/</link>
		<comments>http://albertfama.com/php/error-reporting/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 20:30:44 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[Content]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://albertfama.com/?p=924</guid>
		<description><![CDATA[PHP provides numerous error configuration options in the php.ini file. Since many people do not have the option of editing their ini file I would like to concentrate this series of posts on the ones which can be set within scripts (at run-time), more specifically ones you should consider using when creating a strategy for [...]]]></description>
			<content:encoded><![CDATA[<p>PHP provides numerous error configuration options in the php.ini file. Since many people do not have the option of editing their ini file I would like to concentrate this series of posts on the ones which can be set within scripts (at run-time), more specifically ones you should consider using when creating a strategy for handling errors. If you would like a complete list of the configuration options within PHP, the manual provides a very nice <a href="http://www.php.net/manual/en/errorfunc.configuration.php" title="PHP Manual: Errors and Logging Configuration Options" >chart</a>.</p>
<p>The configuration option I would like to address in this article is, of course, error_reporting.</p>
<p>Let&#8217;s start digging.</p>
<h4>error_reporting</h4>
<p>The error_reporting directive (within the PHP ini file) allows you to set the level of errors reported by PHP. By default this is set to &#8216;E_ALL &amp; ~E_NOTICE&#8217;. This <em>&#8216;tells&#8217;</em> PHP to report all errors, <em><strong>except</strong></em> Notices.</p>
<p>PHP provides a built-in function for getting and setting the error_reporting level: <a href="http://www.php.net/manual/en/function.error-reporting.php" title="PHP Manual: error_reporting()" >error_reporting()</a>. The function signature looks like this:</p>
<p><code>int error_reporting  ([ int $level  ] )</code></p>
<p>The function accepts one optional argument, a new error reporting level, and returns the old error reporting level. Although the signiture shows that the function accepts an integer, the new level sent can be represented by a <a href="http://www.php.net/manual/en/errorfunc.constants.php"  title="PHP Manual: Predefined Constants">predefined constant</a>. In fact it is recommended that you use the predefined constants to ensure future compatibility.</p>
<p>The available constants can be used in combination to build up a <a rel="nofollow" href="http://en.wikipedia.org/wiki/Bitmask"  title="Wikipedia: Bitmask">bitmask</a>; the bitmask <em>tells</em> PHP which errors to report and which to suppress. Since these constants are actually bitmasks, you will need to use <a href="http://www.php.net/manual/en/language.operators.bitwise.php"  title="PHP Manual: Bitwise Operators">bitwise operators</a> when combining them.</p>
<p>An example of the error reporting setting I use in development is:</p>
<pre name="code" class="php">
$old_setting = error_reporting(E_ALL | E_STRICT);
</pre>
<p>From the predefined constants chart the setting E_ALL equates to the integer 30719 and means:</p>
<blockquote><p>All errors and warnings, as supported, except of level E_STRICT in PHP < 6. </p></blockquote>
<p>From the predefined constants chart the setting E_STRICT equates to the integer 2048 and means:</p>
<blockquote><p>Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. </p></blockquote>
<p>So basically, this tell PHP to report all errors, notices, and messages generated at the E_STRICT level.</p>
<p>Back to the code&#8230;</p>
<p>The variable $old_setting will now be set to: 6135, the default setting for error reporting (using the predefined constants again that would be: E_ALL &amp; ~E_NOTICE). </p>
<p>A small code example:</p>
<pre name="code" class="php">
&lt;?php
//get the current error reporting setting
$current_setting = error_reporting();

//check if error reporting is set to E_ALL | E_STRICT
if (8191 != $current_setting) {
    //not set to E_ALL | E_STRICT make it so
    error_reporting(E_ALL | E_STRICT);
}

//display new error reporting setting

echo error_reporting(); //displays 8191
?>
</pre>
<p>A more useful example would be to change the error_reporting setting based on the environment the script is being run in. </p>
<p>Let&#8217;s say you have a configuration file which defines the constant &#8216;ENV&#8217;, this can be set to &#8216;production&#8217; (live site) or &#8216;development&#8217;. If the script is run in production then we want to use &#8216;E_ALL &amp; ~E_NOTICE&#8217; (default), but when running in development we want to use E_ALL | E_STRICT.</p>
<p>config.php:</p>
<pre name="code" class="php">
&lt;?php

//define enviroment
define('ENV', 'development');
</pre>
<p>Script File:</p>
<pre name="code" class="php">
&lt;?php

//load configuration file
require_once('config.php');

//check ENV constant to see if running in development
if ('development' == ENV) {
    //running in development so change default setting
    error_reporting(E_ALL | E_STRICT);
    //now all errors, notices, and suggestions will be reported
}
</pre>
<p>That&#8217;s it! I&#8217;m sure you can see how useful this function can be.</p>
<blockquote><p><strong>Side Note:</strong> If you are unsure what a bitmask is or are unfamiliar with bitwise operators, you may want to checkout this <a href="http://www.joestump.net/2004/06/a-quick-bitmask-howto-for-programmers.html"  title="joestump.net: A Quick Bitmask HOWTO for Programmers">post</a> from <a href="http://www.joestump.net/"  title="joestump.net">joestump.net</a>.</p></blockquote>
</blockquote>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=924">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/error-reporting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A New Start: Error Reporting</title>
		<link>http://albertfama.com/php/new-start-error-reporting/</link>
		<comments>http://albertfama.com/php/new-start-error-reporting/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 17:35:12 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=34</guid>
		<description><![CDATA[A few years back I had written a post about deciphering error messages, which still remains popular today. Because of the continued interest I have decided to update the original post and create a series on error reporting and handling in PHP.
When I first started learning about errors and error handling I found numerous tutorials [...]]]></description>
			<content:encoded><![CDATA[<p>A few years back I had written a post about deciphering error messages, which still remains popular today. Because of the continued interest I have decided to update the original post and create a series on error reporting and handling in PHP.</p>
<p>When I first started learning about errors and error handling I found numerous tutorials on the topic, but none that provided a complete resource on the subject. This is understandable; since the topic is so broad one tutorial would not only be extremely long, it might also seem overwhelming to someone who just started learning about the topic. This is also the reason it would make a great topic to cover on a blog, where a series of posts can be created, each building on the information presented in the previous post ending with a comprehensive  resource. (OK, comprehensive maybe a little strong but hopefully it will be close.)</p>
<p>The intended outline for the series is:</p>
<h5>Resource Information</h5>
<ul>
<li><a title="Errors and Error Messages Deciphered " href="/php/errors-and-error-messages-deciphered/">Error Messages Deciphered (Revised)</a></li>
<li><a title="Error Reporting" href="/php/error-reporting/">Error Reporting</a></li>
<li>Displaying Errors</li>
<li>Error Logging</li>
<li>Backtrace Functions</li>
<li>Triggering Errors
  </li>
<li>Understanding Exceptions</li>
</ul>
<h5>Implementation</h5>
<ul>
<li>Creating A Custom Error Handler</li>
<li>Utilizing Exceptions</li>
<li>Creating an Exception Hierarchy</li>
</ul>
<h5>Putting it all together</h5>
<ul>
<li>Error Handling Strategies</li>
</ul>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=34">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/new-start-error-reporting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NOTICES mean something</title>
		<link>http://albertfama.com/php/notices-mean-something/</link>
		<comments>http://albertfama.com/php/notices-mean-something/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 06:34:36 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[environment]]></category>
		<category><![CDATA[error messages]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://albertfama.com/?p=264</guid>
		<description><![CDATA[In numerous books and articles the general consensus seems to be that for a production environment Errors and Warnings should be turned on and Notices  turned off; as opposed to the development environment where error reporting should be set to E_ALL (report all errors). The reasoning behind this is, Errors and Warnings mean something [...]]]></description>
			<content:encoded><![CDATA[<p>In numerous books and articles the general consensus seems to be that for a production environment Errors and Warnings should be turned <strong>on</strong> and <a href="/php/errors-and-error-messages-deciphered/#notice">Notices</a>  turned <strong>off</strong>; as opposed to the development environment where error reporting should be set to E_ALL (report all errors). The reasoning behind this is, Errors and Warnings mean something serious has gone wrong with the script and you (as the developer) need to know about it. Notices on the other hand are kind of disregarded as a nuisance in production.</p>
<p>I completely disagree with this point of view. If I write code which is not intended to generate Notices, then as the developer I want to know if and when Notices are being generated in <strong>any</strong> environment.</p>
<p>Notices are very helpful in tracking down logical errors or bugs, which cause the script not to function as expected.  In PHP they are normally generated when using a previously undeclared variable. Technically speaking there is nothing wrong with this in PHP, other programming languages are not so forgiving. </p>
<p>I personally started declaring all variables when I began to focus on the security implications of using undeclared variables (we&#8217;ll save that information for another post). After getting in the habit I found that I was able find logical errors during development even before I noticed that a bug existed. After realizing this, I now never release code which is know to generate notices. I have also started setting error reporting to E_ALL in <strong>all</strong> environments.</p>
<p>I understand that this is not feasible for everyone. It may not be possible in a professional working environment because of dealing with legacy code. Another issue could arise if you use third-party code which has not subscribed to this way of thinking. Although personally, I feel this is not an excuse there is no reason you can&#8217;t get in there and &#8216;fix&#8217; their script.  </p>
<h5>A word of warning</h5>
<p>I was once hired to fix numerous bugs on a site, all were a fairly obvious fix, but one in particular had me stumped.  I turned on notices when attempting to debug the problem and ended up with an error log that contained over 250 notices for every page load. Since this was a dev environment setup by the client for my use, it was no big deal, but if it were a heavily trafficked live site, there could have been some serious implications.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=264">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/notices-mean-something/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
