<?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>Web Design company - Goweb99 blog &#187; php error handling</title>
	<atom:link href="http://www.goweb99.com/blog/tag/php-error-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.goweb99.com/blog</link>
	<description>Get your website designed for as low as $99.</description>
	<lastBuildDate>Fri, 30 Jul 2010 20:27:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Error Handling- Creating a Custom Error Handler</title>
		<link>http://www.goweb99.com/blog/php-tutorial/php-error-handling-creating-a-custom-error-handler/</link>
		<comments>http://www.goweb99.com/blog/php-tutorial/php-error-handling-creating-a-custom-error-handler/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 05:10:32 +0000</pubDate>
		<dc:creator>tutor</dc:creator>
				<category><![CDATA[PHP Tutorial]]></category>
		<category><![CDATA[creating custom error handler]]></category>
		<category><![CDATA[php error handling]]></category>

		<guid isPermaLink="false">http://www.goweb99.com/blog/?p=630</guid>
		<description><![CDATA[Creating a Custom Error Handler
A Custom Error Handler is a special function which is called when an error occurs in PHP.
This function should accept at least two parameters, ie error level and error message and at the max it may accept up to five parameters ie file, line-number, and the error context:
Syntax
 error_function(error_level,error_message,
error_file,error_line,error_context)




Parameter
Description


error_level
Required. Specifies the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Creating a Custom Error Handler</strong></p>
<p>A Custom Error Handler is a special function which is called when an error occurs in PHP.</p>
<p>This function should accept at least two parameters, ie error level and error message and at the max it may accept up to five parameters ie file, line-number, and the error context:</p>
<p><strong>Syntax</strong></p>
<ul> error_function(error_level,error_message,<br />
error_file,error_line,error_context)</ul>
<ul>
<table border="0" cellpadding="5" width="100%">
<tbody>
<tr>
<td><strong>Parameter</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr>
<td>error_level</td>
<td>Required. Specifies the error report level for the user-defined error. Must be a value number.</td>
</tr>
<tr>
<td>error_message</td>
<td>Required. Specifies the error message for the user-defined error</td>
</tr>
<tr>
<td>error_file</td>
<td>Optional. Specifies the filename in which the error occurred</td>
</tr>
<tr>
<td>error_line</td>
<td>Optional. Specifies the line number in which the error occurred</td>
</tr>
<tr>
<td>error_context</td>
<td>Optional. Specifies an array containing every variable, and their values, in use when the error occurred</td>
</tr>
</tbody>
</table>
</ul>
<p><strong>Error Report levels</strong></p>
<p>These error report levels are the different types of error the user-defined error handler can be used for:</p>
<ul>
<table border="0" cellpadding="5" width="100%">
<tbody>
<tr>
<td><strong>Value</strong></td>
<td><strong>Constant</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr>
<td>2</td>
<td>E_WARNING</td>
<td>Non-fatal run-time errors. Execution of the script is not halted</td>
</tr>
<tr>
<td>8</td>
<td>E_NOTICE</td>
<td>Run-time notices. The script found something that might be an error, but could also happen when running a script normally</td>
</tr>
<tr>
<td>256</td>
<td>E_USER_ERROR</td>
<td>Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()</td>
</tr>
<tr>
<td>512</td>
<td>E_USER_WARNING</td>
<td>Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()</td>
</tr>
<tr>
<td>1024</td>
<td>E_USER_NOTICE</td>
<td>User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()</td>
</tr>
<tr>
<td>4096</td>
<td>E_RECOVERABLE_ERROR</td>
<td>Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())</td>
</tr>
<tr>
<td>8191</td>
<td>E_ALL</td>
<td>All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)</td>
</tr>
</tbody>
</table>
</ul>
<p>Now lets create a function to handle errors:</p>
<ul>
<div class="example">function customError($erroeNo, $errorString)<br />
{echo &#8220;&lt;b&gt;Error:&lt;/b&gt; [$errorNo] $errorString&lt;br /&gt;&#8221;;<br />
die();<br />
}</div>
</ul>
<p>The code above is a simple error handling function. When it is triggered, it gets the error level and an error message. It then outputs the error level and message and terminates the script.</p>
<p>Here we have created an error handling function, so mow we we need to decide when it should be triggered.</p>
<p><strong>Set Error Handler</strong></p>
<p>PHP provides a built in default error handler error handler.</p>
<p>You can also change the error handler to apply for only some errors, so this way you can make more handlers for different type of errors, which will handle the errors in specific ways.</p>
<ul> set_error_handler(&#8221;customError&#8221;);</ul>
<p>Since we want our custom function to handle all errors, the set_error_handler() only needed one parameter, a second parameter could be added to specify an error level.</p>
<p>In this example below we are testing the error handler by trying to output variable that does not exist:</p>
<ul>
<div class="example">For eg:</p>
<p>&lt;?php    //error handler function<br />
function customError($errorNo, $errorString)<br />
{<br />
echo &#8220;&lt;b&gt;Error:&lt;/b&gt; [$errorNo] $errorString&#8221;;<br />
}<br />
//set error handler<br />
set_error_handler(&#8221;customError&#8221;);<br />
//trigger error<br />
echo($num);<br />
?&gt;</p></div>
</ul>
<ul>
<div class="example">Output:</p>
<p>Error: [8] Undefined variable: num</p></div>
</ul>
<p><strong>Trigger an Error</strong></p>
<p>In the above example we triggered the error without any reason. We need some valid trigger for the error handler to be triggered. When a user inputs data, some wrong input may be provided so we need to trigger the handler whenever a user inputs a invalid data. In PHP, this is done by the trigger_error() function.</p>
<p>In this example an error occurs if the &#8220;num&#8221; variable is less than &#8220;1&#8243;:</p>
<ul>
<div class="example">For eg:</p>
<p>&lt;?php<br />
$num=0;<br />
if ($num&lt;1)<br />
{<br />
trigger_error(&#8221;Number must be greater than 0&#8243;);<br />
}<br />
?&gt;</p></div>
</ul>
<ul>
<div class="example">Output:</p>
<p>Notice: Number must be greater than 0 in C:\applications\errorTest.php on line 6</p></div>
</ul>
<p>By this method you can trigger an error anywhere you wish in a script. By adding a second parameter, you can specify what error level is triggered.</p>
<p><strong>Possible error types:</strong></p>
<ul>
<li>E_USER_ERROR &#8211; Fatal user-generated run-time error. Errors that can not be recovered from. Execution of the script is halted</li>
<li>E_USER_WARNING &#8211; Non-fatal user-generated run-time warning. Execution of the script is not halted</li>
<li>E_USER_NOTICE &#8211; Default. User-generated run-time notice. The script found something that might be an error, but could also happen when running a script normally</li>
</ul>
<p>In this example an E_USER_WARNING occurs if the &#8220;num&#8221; variable is less than &#8220;1&#8243;. If an E_USER_WARNING occurs we will use our custom error handler and end the script:</p>
<ul>
<div class="example">For eg:</p>
<p><strong>&lt;?php   //error handler function<br />
function customError($errorNo, $errorString)<br />
{<br />
echo &#8220;&lt;b&gt;Error:&lt;/b&gt; [$errorNo] $errorString&lt;br /&gt;&#8221;;<br />
die();<br />
}<br />
//set error handler<br />
set_error_handler(&#8221;customError&#8221;,E_USER_WARNING);<br />
//trigger error<br />
$num=0;<br />
if ($test&lt;1)<br />
{<br />
trigger_error(&#8221;Number must be greater than 1&#8243;,E_USER_WARNING);<br />
}<br />
?&gt;</strong></div>
<p><strong> </strong></ul>
<p><strong> </strong></p>
<ul><strong> </strong></p>
<div class="example"><strong> Output:<br />
Error: [512] Value must be 1 or below</strong></div>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.goweb99.com/blog/php-tutorial/php-error-handling-creating-a-custom-error-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Error Handling- die() Statement</title>
		<link>http://www.goweb99.com/blog/php-tutorial/php-error-handling/</link>
		<comments>http://www.goweb99.com/blog/php-tutorial/php-error-handling/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 12:13:13 +0000</pubDate>
		<dc:creator>tutor</dc:creator>
				<category><![CDATA[PHP Tutorial]]></category>
		<category><![CDATA[die() statement]]></category>
		<category><![CDATA[php die statement]]></category>
		<category><![CDATA[php error handling]]></category>

		<guid isPermaLink="false">http://www.goweb99.com/blog/?p=623</guid>
		<description><![CDATA[PHP provides a default error handling, which is very simple. An error message is sent to the browser, with filename, line number and a message describing the error.
Error handling is a vital part while working with scripts and creating web applications.
Error checking and handling helps you handle the errors your way, ie you can do [...]]]></description>
			<content:encoded><![CDATA[<p>PHP provides a default error handling, which is very simple. An error message is sent to the browser, with filename, line number and a message describing the error.</p>
<p>Error handling is a vital part while working with scripts and creating web applications.</p>
<p>Error checking and handling helps you handle the errors your way, ie you can do what you want to do in case an error occurs. It makes your code look more professional and also provides security.</p>
<p>This tutorial contains some of the most common error checking methods in PHP.</p>
<p>Some of the most common error checking methods of PHP are:</p>
<ul>
<li>Simple &#8220;die()&#8221; statements</li>
<li>Custom errors and error triggers</li>
<li>Error reporting</li>
</ul>
<p><strong>Using the die() function</strong></p>
<p>The first example shows a simple script that opens a text file:</p>
<ul>
<div class="example">&lt;?php<br />
$file=fopen(&#8221;welcome.txt&#8221;,&#8221;r&#8221;);<br />
?&gt;</div>
</ul>
<ul>
<div class="example">Error Output if the file does not exist:</p>
<p>Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:No such file or directory in C:\webfolder\test.php on line 2</p></div>
</ul>
<p>You can avoid this situation by simple methods. First look for the file if it exists, then open it and if not then you can use the die() function.</p>
<ul>
<div class="example">&lt;?php<br />
if(!file_exists(&#8221;welcome.txt&#8221;))<br />
{<br />
die(&#8221;File not found&#8221;);<br />
}<br />
else<br />
{<br />
$file=fopen(&#8221;welcome.txt&#8221;,&#8221;r&#8221;);<br />
}<br />
?&gt;</div>
</ul>
<ul>
<div class="example">Error Output if the file does not exist:</p>
<p>File not found</p></div>
</ul>
<p>The code above is more efficient than the earlier code, because it uses a simple error handling mechanism to stop the script after the error.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.goweb99.com/blog/php-tutorial/php-error-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
