{"id":1307,"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\/"},"modified":"2018-01-19T19:24:41","modified_gmt":"2018-01-19T19:24:41","slug":"error-handling-in-php-5","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/error-handling-in-php-5\/","title":{"rendered":"Error Handling in PHP 5"},"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<p>In this article we will be looking at how to handle errors in PHP. Errors are an inevitable part of software development, and accordingly, we will be looking at the various error types and demonstrating how to handle them.<\/p>\n<p>If you intend to run any of the sample scripts in this article, please make sure that display errors is turned on in your PHP initialization document. This section looks something like this:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">\u00a0\u00a0\u00a0 display_errors = On [Security]\n\u00a0\u00a0\u00a0 ; With this directive set to off, errors that occur during the execution of\n\u00a0\u00a0\u00a0 ; scripts will no longer be displayed as a part of the script output, and thus,\n\u00a0\u00a0\u00a0 ; will no longer be exposed to remote users. With some errors, the error message\n\u00a0\u00a0\u00a0 ; content may expose information about your script, web server, or database\n\u00a0\u00a0\u00a0 ; server that may be exploitable for hacking. Production sites should have this\n\u00a0\u00a0\u00a0 ; directive set to off.<\/pre>\n<\/div>\n<p>The reason for this is that, when display errors are turned on, PHP will display all errors that occur during script execution. If however, the directive is switched off, we will not be able to see any errors as the browser will be blank.<\/p>\n<h2>Types of PHP Errors<\/h2>\n<p>PHP has three types of errors:<\/p>\n<ul>\n<li>Syntactical<\/li>\n<li>Run time<\/li>\n<li>Logical<\/li>\n<\/ul>\n<h2>Syntactical PHP Errors<\/h2>\n<p>Of these three, syntactical errors are the most common and also easiest to fix. A syntactical error occurs when code is not properly formulated, for example, if you leave out a semicolon from the end of a code block or if you do not close the bracket in an if-statement. The code below is erroneous because it does not terminate the code with a semicolon:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=iso-8859-1\" \/&gt;\n&lt;title&gt;Untitled Document&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;?php\n\n$i = \"jack\"\nif($i &lt;&gt; \"jack\"){\necho \"Right\"\n}else{\necho \"Wrong\"\n}\n?&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<\/div>\n<p>The code produces the following error:<\/p>\n<p><a href=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/syntaxerror.png\" target=\"_blank\"> <br \/><img loading=\"lazy\" decoding=\"async\" height=\"102\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/syntaxerror.png\" width=\"338\"\/><br \/><span style=\"font-size: xx-small;\"><em>Click here for larger image<\/em><\/span><\/a> <br \/><span style=\"font-size: x-small;\"><strong>Figure 1.<\/strong> PHP 5 Syntax Error<\/span><\/p>\n<p>The reason why we are getting a syntax error is because the first line of code after the code tag does not terminate with a semicolon(;). Syntactical errors are fairly easy to fix, unlike the other types of errors you will encounter. Syntactical errors stop a script from executing. You will also notice that the error message is vague. It does not specifically point to the exact place where the error occurs.<\/p>\n<h2>Runtime PHP Errors<\/h2>\n<p>Unlike syntactical errors, runtime errors actually allow a script to execute but will not allow it to do everything it is supposed to do. For example, calling a function with fewer parameters than it expects will produce a runtime error. Take a look at the code below:<\/p>\n<div id=\"code\">\n<pre class=\"brush: js;\">&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=iso-8859-1\" \/&gt;\n&lt;title&gt;Untitled Document&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;?php\n\nfunction showAge($myname,$age){\necho $myname. \" is\u00a0 \".$age. \" old.\";\n}\n\nshowAge(\"Jack\")\n?&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<\/div>\n<p>The code produces the following runtime error:<\/p>\n<p><a href=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/runtimeerror.png\" target=\"_blank\"> <br \/><img loading=\"lazy\" decoding=\"async\" height=\"102\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/runtimeerror.png\" width=\"339\"\/><br \/><span style=\"font-size: xx-small;\"><em>Click here for larger image<\/em><\/span><\/a> <br \/><span style=\"font-size: x-small;\"><strong>Figure 2.<\/strong> PHP 5 Runtime Error<\/span><\/p>\n<p>Where runtime errors are concerned, PHP tells you exactly what the problem is. For instance, in our example we only included one parameter in the function called <code>showAge()<\/code>, which expects two parameters. The error tells you that the second argument is missing from the <code>showAge()<\/code> function. Also note that the error statement continues with a &#8220;Notice&#8221; section that continues to execute the code.<\/p>\n<h2>Logical PHP Errors<\/h2>\n<p>Logical errors by all accounts are the worst because:<\/p>\n<ul>\n<li>PHP does not always report them to you.<\/li>\n<li>Bugs are not obvious and the script will continue to execute.<\/li>\n<\/ul>\n<p>Normally the only way you discover logical errors is when the code does not produce the desired results.<\/p>\n<h2>Handling PHP 5 Errors<\/h2>\n<p>Basic steps you should follow when debugging are as follows (they&#8217;ve always worked for me):<\/p>\n<ol>\n<li>Name all pages in your project uniquely and in such a way that they identify its purpose and function. For example, a page that list names can be called <em>list_names<\/em> or <em>list_of_names<\/em>, this already tells you what the page is supposed to do and also ensures that you run the correct page\/script. Sometimes, when you try to fix a problem, no matter what you do, it just does not go away. Most of the time, this is because you are working on the wrong script.<\/li>\n<li>Make sure you run your scripts through a URL such as <em>http:\/\/localhost\/list_names.php<\/em> (on your development machine) or <em>http:\/\/www.mywebsite.com\/list_names.php<\/em> (on the Web).<\/li>\n<li>Do preventative coding. For example when creating a if-statement, write the skeleton of the statement:\n<div id=\"code\">\n<pre class=\"brush: js;\">\u00a0\u00a0\u00a0\u00a0\u00a0 If(){\n\u00a0\u00a0\u00a0\u00a0\u00a0 }else{\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\/\/end of statement<\/pre>\n<\/div>\n<p>Then add the code that you want to add. This way you will avoid getting a whole lot of syntactical errors.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Leidago_Noabeb030620124658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Leidago_Noabeb030620124658.html?page=2\">Next Page \u00bb<\/a><\/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-1307","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1307","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=1307"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1307\/revisions"}],"predecessor-version":[{"id":2156,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1307\/revisions\/2156"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}