PHPBuilder - Error Handling in PHP 5



RSS Twitter
Articles Tricks And Hacks

Error Handling in PHP 5

by: J. Leidago Noabeb
|
April 16, 2012

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.

 

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:

 

    display_errors = On [Security]
    ; With this directive set to off, errors that occur during the execution of
    ; scripts will no longer be displayed as a part of the script output, and thus,
    ; will no longer be exposed to remote users. With some errors, the error message
    ; content may expose information about your script, web server, or database
    ; server that may be exploitable for hacking. Production sites should have this
    ; directive set to off.

 

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.

 

Types of PHP Errors

 

PHP has three types of errors:

  • Syntactical
  • Run time
  • Logical

 

Syntactical PHP Errors

 

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:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php

$i = "jack"
if($i <> "jack"){
echo "Right"
}else{
echo "Wrong"
}
?>
</body>
</html>

 

The code produces the following error:

 



Click here for larger image

Figure 1. PHP 5 Syntax Error

 

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.

 

Runtime PHP Errors

 

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:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php

function showAge($myname,$age){
echo $myname. " is  ".$age. " old.";
}

showAge("Jack")
?>
</body>
</html>

 

The code produces the following runtime error:

 



Click here for larger image

Figure 2. PHP 5 Runtime Error

 

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 showAge(), which expects two parameters. The error tells you that the second argument is missing from the showAge() function. Also note that the error statement continues with a "Notice" section that continues to execute the code.

 

Logical PHP Errors

 

Logical errors by all accounts are the worst because:

 

  • PHP does not always report them to you.
  • Bugs are not obvious and the script will continue to execute.

 

Normally the only way you discover logical errors is when the code does not produce the desired results.

 

Handling PHP 5 Errors

 

Basic steps you should follow when debugging are as follows (they've always worked for me):

 

  1. 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 list_names or list_of_names, 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.
  2. Make sure you run your scripts through a URL such as http://localhost/list_names.php (on your development machine) or http://www.mywebsite.com/list_names.php (on the Web).
  3. Do preventative coding. For example when creating a if-statement, write the skeleton of the statement:
          If(){
          }else{
          }//end of statement

     

    Then add the code that you want to add. This way you will avoid getting a whole lot of syntactical errors.


1
|
2
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
J. Leidago Noabeb

Comment:



Comment:

(Maximum characters: 1200). You have characters left.