How to Debug PHP Crashes

How to Debug PHP Crashes thumbnail
Solicit help from fellow programmers to find the cause of a PHP crash.

When a PHP program crashes, it stops execution in the middle of processing because of an unexpected error. PHP crashes can be serious, because they can leave data partially processed or updated and abruptly close open files when the crash occurs. The cause of a PHP crash can be an error in the PHP script or might be due to bad data, rather than a programming error. There are several things you can do when PHP crashes to identify the cause of the crash. Then you can either fix incorrect PHP script or include extra script that anticipates and traps errors caused by bad data.

Instructions

    • 1

      Review the web server's log file. Locate the file, which under Apache is typically located at /var/log/apache2/error.log. Display the most recent portion of the error log to the screen by typing:

      tail /var/log/apache2/error.log

    • 2

      Review the error messages in the log file. Open the PHP script in an editor and review the code that corresponds with the line number cited in the error message. Look for errors in logic that might have caused the crash to occur. Examine at least 10 lines above and below the line number cited in the error, since the interpreter might report an error on a different line from the line where it is actually contained.

    • 3

      Turn error reporting back on so you can interactively debug the script without having to review the log file after each step. Use an editor to modify the PHP script. Comment out a line that has the "error_reporting" statement and add a new line below it with the following statement:

      error_reporting(E_ALL);

    • 4

      Insert debugging code into the PHP script so you can more easily track down errors that are caused by bad or unexpected data. Create a function to write debugging messages to the screen and create a Boolean variable that allows you to globally turn error reporting on and off. Insert debugging statements in the PHP code. For example, add the following code to the PHP script:

      $debug = true;
      function debug($msg) {
      global $debug;
      if (!$debug) return;
      echo "<p>*** $msg ***</p>";
      }

      Insert the debug function around where the error occurred. For example, type:

      debug("x = $x and y = $y after processing the first data item");

      Change the $debug variable to false when you no longer want to write debugging messages to the screen.

    • 5

      Use "try/catch" statements or "if-else" logic to trap errors that might be caused by unexpected conditions such as bad data. Trap errors that can cause a crash even when data should never contain the values that would cause the crash to occur. For example, type the following to avoid a "divide by zero" error even if a zero is not a valid value for the variable:

      $average = 0;
      if ($count != 0) {
      $average = $total / $count;
      }

    • 6

      Enlist the help of fellow developers who might not be as familiar with the code. Sometimes the cause of an error can be an assumption you make due to your familiarity with the way the program works. After you find the cause of the crash, brainstorm with fellow developers about other crashes that could be caused by similar logic errors or data conditions. Review the entire PHP script and fix other areas of the program that might be vulnerable to similar crashes.

Related Searches:

References

  • Photo Credit BananaStock/BananaStock/Getty Images

Comments

Related Ads

Featured