Debugging Code

When you make lots of errors, you learn what causes them. Nonetheless, you will still make some errors that you can't fix right away. Here are some debugging tips.

READ THE ERROR MESSAGE

Il va sans dire (it goes without saying). You may get lucky. The error message may be right on point and lead you directly to the problem. Furthermore, once you've done this a number of times, you begin to recognize what causes certain error messages without further debugging.

USE VIEW-SOURCE

PHP processes code, then converts it into HTML. You know what you want the output to be, so go to view-source and see if it is what you expected.

If you set $first = 'red', and you wanted to code: <span style='color:<?= $first; ?>'>, but your text remains black, do a view-source on the page. You may find your processed code reads <span style='color:$first;'>. This would cause no error message, since the PHP processor sees nothing wrong. In addition, your browser won't know what color:$first means, so it will be ignored.

USE DIE();

die(); is a handy function for debugging. It kills the processing at that point and prints a message, which is inserted as an attribute to the function.

To use die(); effectively, go to the earliest portion of your code that could possibly cause the error, which may be the very beginning of the file, and insert this function with an appropriate message.

For instance, you may instantiate a variable. It's probably safe to say there is no error in $i=0; so proceed further. The variable changes value for whatever reason. Now is a good point to use die();. Right after the point where the value changes, add die($i);. Save the file and refresh your browser. The processing will stop at that point and the browser will display the value of $i. If it is as expected, continue to another place in the file. If it is not as expected, you have narrowed the possible causes of error.

Another example would be to add die(); in your loop somewhere. If the condition is false when you know it should be true, and you insert die('condition is false'); in the code corresponding to a false condition, and the processing halts with that message, now you know something.

USE ECHO();

Similar to using die();, echo(); will print messages for you, only the processing of code will not stop. Use echo(); generously throughout the trouble spots in your code, and you will get a readout from several places in the code at once.

DO NOT EDIT A 'LIVE' FILE

The consequences are obvious in that anyone who happens to be viewing your page in the middle of your debugging will get bizarre results. If you absolutely must make revisions live, consider doing something like the following:

<?php
$numfields = mysql_num_fields($result);
  echo "<table cellpadding='2' cellspacing='0' border='1'>\n";
  echo "\t<tr>\n";
  for ($i=0; $i<$numfields; $i++) { 
    echo ("\t\t<th>".mysql_field_name($result,$i)."</th>\n"); 
  }
  echo "\t</tr>\n";
/////////////////suppose you suspect a problem with the while loop below///////// //////////insert a conditional here that will not affect any other viewers///////
if ($_SERVER["REMOTE_ADDR"]=='3.144.35.148') {
  $answer = mysql_fetch_array($result);
	echo $answer['nameLast']."<br />";
	echo $answer['nameFirst']."<br />";
	echo $answer['MI']."<br />";
	die();
} else {
////////////////////continue with normal coding here/////////////////////////////
while ($answer = mysql_fetch_array($result)) {
    echo "\t<tr>\n";
    for ($k=0; $k<$numfields; $k++) { 
      echo ("\t\t<td>" .$answer[$k]."</td>\n"); 
    }
    echo ("\t</tr>\n"); 
  }
} ////////////close your temporary conditional here//////////////////////////////
echo "</table>\n";
?>
Insert your own value for $_SERVER["REMOTE_ADDR"]. Now, only you will see the results of your debugging.

CONTROLLING ERROR MESSAGES

PHP has a pre-defined function to control what error messages will display. error_reporting() takes one of twelve possible integers as an attribute. Call this function very early in your program, like the first thing.

While your program is in a development mode, it is a good idea to set error_reporting(8);. This displays all errors, warnings, notices--basically everything you could repair. When you are ready to go live with your product, set error_reporting(1);, which displays only fatal errors.

Warnings are possible problems, but not enough to prevent processing. All programs have lots of warnings. Don't be overly concerned with all the warning messages associated with your program. There is probably nothing wrong.

You can also suppress error messages using @. Certain programming processes that can generate error messages, such as a function call, can use @ to prevent the message from displaying. Note: whatever error occurs will still cause the same consequences (like killing the processing), just no message will appear. This is handy if you don't want the viewer to see the message, or if you want to generate your own message (which is an advanced programming skill not taught in this course, but here is an example). Here is a sample of its use:

$newNumber = @randomNumber();
Now, if your user-defined function randomNumber() causes an error, it will not display.

Error Handling   < <  PREVIOUS   Table of Contents NEXT  > >   Regular Expressions

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,562 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHP10.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 1:37 am, UTC
This file took 0.03366 seconds to process.