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 post breaks down the 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.
Performing your Ground Work
It is essential that you configure PHP correctly and write your code in a way that it produces meaningful errors on the right time. For example, it is actually generally good practice to change on a verbose amount of error reporting on your development platform. This probably isn’t such a great idea, however, on your production server(s). In a live environment you neither wish to confuse a genuine user or give malicious users excessive details about the inner-workings of your site.
So, with that in mind lets discuss the much too common “I’m getting no error message” issue. This can be normally the result of a syntax error on the platform where developer has not done their ground work properly. First, you should turn display_errors on.
Next, you will need to set a mistake reporting level. As default PHP 4 and 5 do not show eth notices which is often crucial in debugging your code (much more on that shortly). Notices are generated by PHP if they are displayed or otherwise not, so deploying code with twenty notices being generated posseses an impact upon the overhead of your own site.
It is also worth mentioning that on your own development platform it is often a smart idea to make these alterations in your php.ini file as opposed to on the runtime. This is because if you experience a syntax error with these options placed in your code and not inside the php.ini you might, depending on your setup, be given a blank page. Likewise, it is actually worth noting that when you’re setting these values in your code, a conditional statement might be a good
Syntactical errors or parse errors are typically the consequence of typo in your code. Such as a missing semicolon, quotation mark, brace or parentheses. Warnings aren’t deal breakers like syntax errors. PHP iqgzff manage a stern warning, however, it recognizes that you almost certainly created a mistake somewhere and is also notifying you about it.
Notices aren’t going to halt the execution of your own code either, but they may be essential in tracking down a pesky bug. Often you’ll discover that code that’s working perfectly happily in a production environment starts throwing out notices whenever you set error_reporting to E_ALL.
PHP has helpfully told us that the FirstName key is undefined so that we understand that this isn’t a case of the database record being NULL. However, perhaps we need to check our SQL statement to make certain we’ve actually retrieved the user’s first name from the database. In cases like this, the notice helps us rule out a possible issue which includes in turn steered us for the likely way to obtain our problem. With no notice our likely first stop could have been the database record, then tracing back through our logic to eventually find our omission within the SQL.
Fatal Errors sound the most painful from the four but are in fact frequently the easiest to solve. What it really means, to put it briefly, is that PHP understands what you’ve asked it to do but can’t carry out the request. Your syntax is correct, you’re speaking its language but PHP doesn’t have what it requires to comply.
Hopefully, during the course of this post you have learned how you can do your groundwork by preparing java for that debugging process; recognize and handle the four key PHP error types and use var_dump() in your favor. Likewise, I hope that you will find Xdebug and FirePHP useful and that they is likely to make life easier for you throughout your development cycle.
As I’ve mentioned above, and I really can’t say this enough, remember to get rid of or suppress your debug output whenever you put your sites into production, all things considered, there’s nothing worse than all your users having the capacity to find out about your errors in excruciating detail.