Error Handling

Errors are inevitable in your code. You probably experienced some already. Even though PHP has built-in means of helping the programmer, to the novice, it can be an exasperating experience. This lesson will give you a few tips in dealing with errors.

The first thing you want to avoid is letting a viewer see any error message. Error messages can be very revealing regarding file locations, variable names, etc. If an amateur hacker happened to see one of the error messages, he could do a lot of damage to your program. Therefore, you have to test and check every page after you do anything to the code to be sure all errors are fixed before you put the site live on the internet again.

Error messages in the wrong place, or at the wrong time, can also shock your viewers. The messages read "Fatal error" and other such morbid text. They develop a deep insecurity among non-programmers.

Error messages may appear somewhere in the code that prevents them from displaying on the screen. All you get is a blank screen and no other clues as to what is wrong. If a screen is blank, start looking in places that do not display on the page. For instance:

<input type="hidden" name="firstName" value=<?= readName(); ?> />
If the function readName() produced an error, the message would not appear on the page.

Although there are many possible errors, they generally fall into two categories: Syntax errors and Logical errors.


These are the most common errors, and are generally caused by one of the following reasons:

  1. Typing mistakes
  2. Unbalanced constructs
  3. Unbalanced/improper quotes
  4. Missing semi-colons
  5. Undefined functions

Things can be misspelled if they are uniformly misspelled (i.e. if they are misspelled the same way everywhere). But to the computer, '$school' is not the same as '$shool'.

Unbalanced constructs means you have three ((( but only two )), or you have six {{{{{{ and seven }}}}}}}. Error messages will read "...expecting a ')' ..." and give a line number. The only problem is the ')' is actually missing from 20-30 lines up the page. The computer keeps going until it reaches a point where the ')' could not possibly be further, and that line is reported in the error message. Whenever one of the constructs is missing, look further up in the code.

Quotes hold a special place in programming. See if you can tell what's wrong with the following:

echo "Today's date is ".$date<br />; missing the ." after $date and the " after the <br />
echo "<table width="100%">"; double quotes within double quotes...change either pair to single quotes

Missing semi-colons is the most common error for most novice programmers.

if ($i<10) {echo "Too small!"} need a semi-colon after small!" and before the }

At least undefined functions are easy errors to find. If you never defined a function and you try to call it, you will get this message. If you defined a function in another file, but not this one, you will get this message. If your code fails for one of the syntax reasons above, and a function is called, it may register as undefined.


The infinite loop was discussed in last lesson. If you have a condition that can never fail, the loop will go on forever. Again, the most common way to cause an infinite loop is to do like so:

for ($i=0; $i<10; $i++) {
  if ($i=5) {echo "yes";} else {echo "no";}
($i=5) should read ($i==5). As is, the loop starts with $i=0, then the code changes it with $i=5, then that iteration ends by increasing $i to 6. The next iteration starts, but the code changes $i back to 5 again, and it repeats indefinitely.

Division by zero happens more often than you may realize. Any time you need to divide anything, test for division by zero as follows:

if ($i != 0) { $h = $k/$i; }

Functions with no return value produce interesting logical errors. Enough said.

function add5($initValue) {$newValue = $initValue + 5;}
$b = 12; $newValue = add5($b);

What result?
add return $newValue to the code within the add5() function and it works.

Good Code Practice   < <  PREVIOUS   Table of Contents NEXT  > >   Debugging Code

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,181 different visits in the last 30 days.
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 8:19 am, UTC
This file took 0.00842 seconds to process.