SDWorkPlace.com

Original article link: http://www.sdworkplace.com/pages/errors/index.html

Errors: examining the world of frustrations

As programmers and web designers, errors are inevitable. Often, time constraints that surround programming projects are concerned not with original code generation, but with code debugging, a large and resource intensive task within many computing jobs. Don't think of errors as problems, but as opportunities to improve your code to ensure correct functioning of your finished project. Part 1 of this article examines 4 types of errors on the Internet, how to recognize them and ultimately how to recover quickly from them. Part 2 takes a look at PHP-specific errors, what they mean and introduces PHP's support for custom error reporting.

The 4 types of Internet errors

Usually, errors on the Internet can be broken down into 4 categories. Let's take a look at each in stride and consider an example of each.

  1. Syntax errors

    These are mistypings or other errors in code that prevent the execution of a script. A syntax error is the only error type that prevents the script from any execution. Let's take a look at an example using PHP.

    -----Syntax Error-----
    $x = 1
    echo $x;

    The above is an error because every executable line within PHP must be terminated with a semicolon. The second line is correct, but the first line is not.

    Other syntax errors include unbalanced parenthesis within mathematical equations, mistyped language keywords and extra curly braces, among many others. Many times, syntax errors are the easiest to diagnose because error messages display the problem and also the problem's location.

  2. Semantic errors

    Semantic errors include technically correct code, but fundamental problems with the meaning of the code. Since compilers often do point to the error, semantic errors can be found and diagnosed in many cases without much heartache. The following example

    -----Semantic Error-----
    include("file.txt");

    would be a semantic error if file.txt did not exist, and therefore could not be included within the document. Please note that although, on the surface, semantic errors can closely resemble syntax errors, a script can be executed until the point of the semantic error, unlike scripts that include syntax errors. This is because semantic errors contain correct code, like our example above. It just so happens that file.txt does not exist, but the compiler has no way of knowing until it reaches that specific line of code.

  3. Logic errors

    The most difficult to diagnose, logical errors contain syntactically and semantically correct code, but does not execute the way the programmer has intended. This is often what drives programmers up a wall, especially with large and complex Internet applications.

    Logic errors can be as simple as wrong operator usage (+ instead of -, / instead of *) to as complex as database record calls and elaborate SQL statements. A logic error in PHP might look something like this:

    -----Logic Error-----
    $States = array ("Alabama", "Alaska", "Arizona",
    "Arkansas", "California");
    echo "The first record in the States array is " . $States[1];

    The 5 states listed in alphabetical order are saved to an array called States. The logic error comes in the 2nd statement, when we intend to output the first record in the array, or Alabama. Experienced programmers will pick this error up immediately, but to those without much knowledge in programming, it may seem cryptic. Simply put, array positions begin with 0, not 1. The 2nd statement will output Alaska, not Alabama like we intended to do. To fix this, we would simply modify the output to $States[0] instead of $States[1].

    Another logic error might be including the wrong file, adding wrong variables or simply forgetting to manipulate data. Compilers cannot find logic errors, as it has no knowledge of what the programmer intends to do. In the above snippet of PHP code, there is nothing wrong with outputting cell position 1 in the array, so the compiler did it without any problem. Therefore, logic errors must be determined and remedied only by the programmer.

  4. Environment errors

    Environment errors are often forgotten, but are very real within the Internet community. Environment errors are errors beyond the programmer's control, and can include absent language components within ASP to register_globals being turned off within PHP. It can be said, however, that environment errors occur least often out of the aforementioned 3 error types.

PHP specific error messages and reporting

PHP error messages, in most cases, are fairly user-friendly. Let's take a look at a few error messages and the code that produced them.

Code

includ("file.txt");

Error

Fatal error: Call to undefined function: includ() in
c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12

Comment

Since includ() is an undefined function, a fatal error (or a semantic error) occurs and execution stops. As you can see, PHP catches the error and details the problem (undefined function: includ()) and even provides the line that the error occurred on.

Since fatal errors are semantic errors, the script does execute up until the error. Since PHP needs to compile and execute each line of code to continue, it will immediately stop upon encountering a fatal error.


Code

$x = 1
echo $x;

Error

Parse error: parse error in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 13

Comment

Using our syntax error example from above, the compiler did indeed catch the missing semicolon in our first statement ($x = 1). Although the error does not specifically state "missing semicolon", a parse error tells the programmer a syntax error has occurred, which allows the programmer to hone his or her debugging skills to a specific type of error.

Remember that since parse errors are syntax errors, no execution was performed on the script. Once the semicolon is added to the first line of code, the script will execute successfully and as intended.


Code

// $x was never declared
echo $x;

Warning

Warning: Undefined variable: x in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12

Comment

As you can see, this is a warning, not an error (in this case, the warning message is also known as a Notice). Warnings occur when PHP determines something is wrong within the script but is not serious enough to cease execution of the script.

It is essential to understand that warnings do not prevent the execution of the script. When a warning is encountered, a warning message will be placed within the output of a script where the line of code was read. PHP will continue executing the remainder of the script until the end of line is reached.

For example, the following code:

$i = 1;
echo $x;
echo $i;

Will produce this output:

Warning: Undefined variable: x in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12
1

Notice the 1 at the end of the output. After PHP encountered the warning on the second executable line ($x not defined), it correctly interpreted and executed the third line.



The three examples above detail the types of error messages present within PHP. In an effort to give the programmer absolute control, PHP allows the programmer the ability to select which errors and warnings to display. If the programmer has access to the PHP.INI file on the host machine, errors can be globally modified. If, however, the programmer wishes to modify output relative to specific files, the error_reporting() function can be utilized.

The error_reporting() function takes one parameter that signifies the type of error/warning output to support and is usually written at the top of any PHP document. The error reporting parameters are as follows:

0 - No error reporting
1 - Fatal errors
2 - Warnings
4 - Parse errors
8 - Notices

So, if you'd rather only display Fatal errors, then the function code looks like this:
error_reporting(1);

What if the programmer wants Fetal errors and Parse errors together? Simply add the two values together.
error_reporting(5);

Throughout this article, we have looked at 4 different types of Internet error messages and what programmers do to fix them. We then looked specifically at PHP error messages, what they mean and how to control which errors to display. Errors are an inevitable fact of life for programmers, and learning to deal with them and understanding effective methods for tackling them can greatly increase productivity and, most importantly, increase the enjoyment of programming.

Author: Steve
Date written: July, 2002