Nobody enjoys the process of debugging their code. In order to build killer web apps though, it’s crucial that you understand the process thoroughly. This article breaks down the basic principles of debugging in Font, allows you to 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 on the proper time. As an example, it really is generally good practice to transform on the verbose amount of error reporting on the 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 real user or give malicious users too much information about the inner-workings of your site.
So, knowing that lets talk about the all too common “I’m getting no error message” issue. This really is normally the result of a syntax error over a platform where the developer has not done their ground work properly. First, you need to turn display_errors on.
Next, you will need to set an error reporting level. As default PHP 4 and 5 tend not to show btc 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 otherwise, 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 the development platform it is usually a good idea to make these modifications in your php.ini file instead of at the runtime. It is because in the event you experience a syntax error with these options occur your code rather than in the php.ini you could, according to your create, be given a blank page. Likewise, it is actually worth noting that if you’re setting these values inside your code, a conditional statement may well be a good
Syntactical errors or parse errors are usually the consequence of typo in your code. For instance a missing semicolon, quotation mark, brace or parentheses. Warnings aren’t deal breakers like syntax errors. PHP iqgzff deal with a stern warning, however, it knows that you probably produced a mistake somewhere and it is notifying you about it.
Notices aren’t likely to halt the execution of the code either, but they may be essential 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 whenever you set error_reporting to E_ALL.
PHP has helpfully told us the FirstName key is undefined so we realize that this isn’t a case in the database record being NULL. However, perhaps we must check our SQL statement to ensure we’ve actually retrieved the user’s first name through the database. In this instance, the notice has helped us eliminate a prospective issue which includes in turn steered us towards the likely way to obtain our problem. Without the notice our likely first stop might have been the database record, accompanied by tracing back through our logic to eventually find our omission inside the SQL.
Fatal Errors sound probably the most painful from the four but actually are usually the easiest to settle. Exactly what it means, to put it briefly, is that PHP understands what you’ve asked it to perform but can’t perform 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 php for that debugging process; recognize and handle the 4 key PHP error types and make use of var_dump() to your advantage. Likewise, Hopefully you will find Xdebug and FirePHP useful and that they is likely to make your life easier during your development cycle.
As I’ve mentioned above, and i also really can’t say this enough, never forget to remove or suppress your debug output when you put your sites into production, all things considered, there’s nothing worse than your users having the ability to learn about your errors in excruciating detail.