|
Introduction to Debugging and Error Checking |
|
|
|
|
Written by Harry
|
|
Wednesday, 23 January 2008 |
|
Page 3 of 3 var_dump() can be useful in determining whether a variable has been set and whether it contains the information you where expecting. The print_r function will recursively print an array. For example: PHP Code: <?php $myArray = array('firstname' => 'Alan', 'surname' => 'Wagstaff', 'email' => '
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
', 'phone' => '12345-678901'); print_r($myArray); In the code above, we create a basic array with a few details in it then use print_r() to echo it. The result will be something like the following: Code: Array ( [firstname] => Alan [surname] => Wagstaff [email] =>
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
[phone] => 12345-678901 ) As you can see, this shows us the array keys and their values in a nicely formatted fashion so we can check what they contain. This is particularly useful when retrieving an array of database results to quickly check you got the data you expected. Error Reporting and Logging When writing and debugging our application, we will always want to display all notices, warnings and errors. Error messages hidden in a log file are not much good to us when debugging  The first thing we need to check is that we are set to display errors. You can do this by opening your PHP.INI and checking that "display_errors" is set to "On". Next, at the top of our script we want to add: PHP Code: error_reporting(E_ALL); This will ensure that PHP shows us all errors, warnings and notices. Conclusion As you have seen, proper error checking in your applications will save vast amounts of time when debugging later. I hope that you have seen the importance of error checking and that to effectively debug an application it is a case of either starting from the top and working down until you find the bug, or starting from the error and working back up until you find the bug. Thankfully we have functions such as mysql_error() and var_dump() to help us in this task.
|
|
Last Updated ( Wednesday, 23 January 2008 )
|