{"id":932,"date":"2010-09-18T06:24:23","date_gmt":"2010-09-18T06:24:23","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=932"},"modified":"2010-09-18T06:39:13","modified_gmt":"2010-09-18T06:39:13","slug":"how-to-debug-php-code","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2010\/09\/how-to-debug-php-code\/","title":{"rendered":"How to debug PHP code?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-936\" title=\"phpmind-phpdebug\" src=\"http:\/\/www.phpmind.com\/blog\/wp-content\/uploads\/2010\/09\/phpmind-phpdebug.jpg\" alt=\"\" width=\"150\" height=\"137\" \/>Debugging PHP code is challenging task\u00a0 for all Php developers, however some simple php debugging techniques can help you to code faster and thus save very valuable coding time.<\/p>\n<h4>Enable Notices on\u00a0 development servers!<\/h4>\n<p>A notice is a type of error message which is less severe than parse, fatal and warning error messages. A notice may tell  you  that you have used that variable without defining initialized it! Enabling notices helps you to do following things &#8230;.<\/p>\n<ul>\n<li> Warns about un-initialized variables and non-existent array indexes.<\/li>\n<li> Identifies deprecated behavior.<\/li>\n<li> Various non-critical issues that could potentially cause problems.<\/li>\n<\/ul>\n<pre lang=\"php\"># Inside PHP.ini\r\nerror_reporting = E_ALL\r\n\r\n# Inside httpd.conf or .htacess\r\nphp_value error_reporting 2047\r\n\r\n# Inside a script\r\n<\/pre>\n<p>If you want to test that notices are turned on you can simply create a file with the following:<\/p>\n<p>Here I would like to remind you that you should always ensure that the display_errors ini setting is set to \u2018Off\u2019 for any live\/production sites.<br \/>\nPart from that PHP 5.0\u00a0 introduces E_STRICT, which is primarily used to indicate functionality that has been deprecated in PHP5.<br \/>\nPHP 5.0 Error Reporting<\/p>\n<pre lang=\"php\"># Inside PHP.ini\r\nerror_reporting = E_ALL | E_STRICT\r\n\r\n# Inside httpd.conf or .htacess\r\nphp_value error_reporting 4095\r\n\r\n# Inside a script\r\n<\/pre>\n<h4>Use a Logging System<\/h4>\n<p>A logging system can be very useful in tracking down bugs, especially when they happen in a production environment. Such a system can also be useful in debugging during development. Users rarely report errors and often may not even notice the erroneous behavior. So it is absolutely critical to log errors to ensure that is a record of the problem.<\/p>\n<pre lang=\"php\"># Inside PHP.ini\r\nerror_log = \/home\/vhost\/logs\/php_error_log\r\ndisplay_errors = Off\r\nlog_errors_max_len = 0\r\n<\/pre>\n<p>As for all the other errors, we need to make sure that they are caught and dealt with properly. Make sure the user is shown a nice error page (with a suitably cute \u2018oops-back-soon\u2019 picture) and then log, log everything in sight!<br \/>\nThe stack current trace (see debug_backtrace() and debug_print_backtrace()).<\/p>\n<ul>\n<li> The output of get_defined_vars(). However, this is only useful if you call it at the point the error occurs, not at the point the error is logged. This includes global variables.<\/li>\n<li> Any and all information about the remote user (IP address, user agent, session data)<\/li>\n<li> All global variable data (which includes the contents of $_COOKIES, $_SERVER etc.)<\/li>\n<li> Any other status data which is specific to your application<\/li>\n<\/ul>\n<h4>Use an Integrated Development Environment and Debugger<\/h4>\n<p>Use a editor with inbuilt live Php debug (Like <strong>Zend Studio, phped ,Phpdesigner, eclipse<\/strong> etc), Editors like editpad, notepad doesn\u2019t support live debugging and thus need to run the php file on browser for finding the errors. These editors run php scripts via command line on every save and present any errors found.<\/p>\n<p>I also use a remote debugger (<strong>ZendDebugger<\/strong>), which ties into the IDE. The remote debugger is a PHP module that allows you to debug code on your server using the IDE on your local machine. You can set breakpoints, inspect variables, examine stack traces, profile code and all the other benefits you would expect from a debugger. And no, Zend does not sponsor me.<\/p>\n<h4>Use Xdebug<\/h4>\n<p>The Xdebug extension allows you to find  stack traces and function traces in error messages, memory allocation and protect from infinite recursions happening<br \/>\nIt also provides profiling information for PHP code and is having the capability to debug scripts interactively with a debug client.<\/p>\n<h4>Use PHP Frameworks<\/h4>\n<p>Php frameworks like Zend, codeignitor and cakephp provides a lot of functionality for setting up test cases, units and debugging. Most of the code  is ran via libraries the error messages are nicely crafted and provides inbuilt details.<\/p>\n<h4>Unit Testing<\/h4>\n<p>Unit testing may not be everyone\u2019s idea of fun, but it I is very good for developing larger projects. It can give you confidence that code is fine, as well as point out problems before your code goes into production.<\/p>\n<h4>Error Tracking with magic constants<\/h4>\n<p>PHP has four magic constants, _FILE_, _LINE_, _FUNCTION_ and _CLASS_ that can be used to backtrack to the source of the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Debugging PHP code is challenging task\u00a0 for all Php developers, however some simple php debugging techniques can help you to code faster and thus save very valuable coding time. Enable Notices on\u00a0 development servers! A notice is a type of error message which is less severe than parse, fatal and warning error messages. A notice [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-932","post","type-post","status-publish","format-standard","hentry","category-advance-php-interview-questions"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=932"}],"version-history":[{"count":8,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/932\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/932\/revisions\/934"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}