Nobody enjoys the process of debugging their code. If you wish to build killer web apps though, it’s crucial that you be aware of the process thoroughly. This article breaks down basic principles of debugging in Font, helps you understand PHP’s error messages and introduces you to some useful tools to help make the process a little less painful.
Doing all of your Ground Work
It is important that you configure PHP correctly and write your code in a manner which it produces meaningful errors in the proper time. For instance, it is actually generally good practice to change on the verbose level of error reporting on your development platform. This probably isn’t such a great idea, however, on your own production server(s). In a live environment you neither desire to confuse a genuine user or give malicious users too much information regarding the interior-workings of your own site.
So, knowing that lets discuss the all too common “I’m getting no error message” issue. This really is normally caused by a syntax error over a platform where developer has not yet done their ground work correctly. First, you ought to turn display_errors on.
Next, you will need to set a mistake reporting level. As default PHP 4 and 5 tend not to show eth notices which can be crucial in debugging your code (more about that shortly). Notices are generated by PHP whether or not they are displayed or not, so deploying code with twenty notices being generated comes with an impact upon the overhead of your site.
Additionally it is worth mentioning that on your own development platform it is often smart to make these alterations in your php.ini file rather than at the runtime. The reason being if you experience a syntax error by using these options occur your code and not in the php.ini you could, based on your setup, be given a blank page. Likewise, it is worth noting that if you’re setting these values within your code, a conditional statement might be a good
Syntactical errors or parse errors are generally the result of a typo in your code. For example a missing semicolon, quotation mark, brace or parentheses. Warnings aren’t deal breakers like syntax errors. PHP iqgzff cope with a stern warning, however, it recognizes that you most likely made a mistake somewhere and is notifying you about it.
Notices aren’t likely to halt the execution of the code either, but they could be extremely important in tracking down a pesky bug. Often you’ll realize that code that’s working perfectly happily in a production environment starts throwing out notices when you set error_reporting to E_ALL.
PHP has helpfully told us the FirstName key is undefined so we understand that this isn’t a case of the database record being NULL. However, perhaps we should check our SQL statement to ensure we’ve actually retrieved the user’s first name from your database. In this instance, the notice helps us exclude a prospective issue that has subsequently steered us to the likely way to obtain our problem. Minus the notice our likely first stop would have been the database record, followed by tracing back through our logic to eventually find our omission within the SQL.
Fatal Errors sound the most painful from the four but actually are often the easiest to solve. What it means, to put it briefly, is the fact that PHP understands what you’ve asked it to accomplish but can’t perform the request. Your syntax is correct, you’re speaking its language but PHP doesn’t have what it must have to comply.
Hopefully, over the course of this short article you might have learned how to do your groundwork by preparing java for your debugging process; recognize and handle the four key PHP error types and use var_dump() to your benefit. Likewise, Hopefully you can find Xdebug and FirePHP useful and that they can make life simpler during your development cycle.
As I’ve already mentioned, and that i really can’t say this enough, remember to remove or suppress your debug output whenever you put your sites into production, in the end, there’s nothing worse than your users being able to find out about your errors in excruciating detail.