Debugging in PHP – Part 1

Debugging is most important in development as it saves your countless hours of coding. Lets explore the various debugging techniques.

1. The basic includes turning on error reporting.

2. The next one involves using print statements. This help you identify elusive bugs by displaying what is actually going on your screen.

3. This one is what i currently play with PHPeclipse which is an Eclipse Plug, that can highlight comman errors and also can be used as a debugger setting up breakpoints. Those who have played J2ee development, would be knowing it much better.

Now lets jump on each of these for a better construing. Its always the best practice to keep error reporting ON while on development mode, as it acts as a defence for developers like us. Make sure its off while the code is in production environment, as you don’t want your visitors to track the error which may lead him to get some loop holes for attacks.

Error reporting via PHP

I hope you would have come across php.in file. This is a configuration file which is used to control some of settings of PHP interpreter. I am in the progress of writing a detailed explanation on what exactly this php.ini is how we can play with it. Keep watching the site for updates. So lets come back to the game, you may notice these few lines in your php.ini file.

display_errors = Off
error_reporting = E_ALL

The main purpose of display_errors varibale is to whether you want to show errors or not. To make life easier set it ON while you are in development mode. What’s all about error_reporting. This displays everything from bad coding practices to harmless notices to errors. basically i would set it as something like this to avoid harmless notices, like uninitialized variables.

error_reporting = E_ALL & ~E_NOTICE

Now restart your apache, and the get start go. Now how about if you don’t have the permissions in your php.ini file. Don’t get fret, here is how we go again.

 <?php
error_reporting  (E_ALL);
ini_set (‘display_errors’, true);
include(‘./filename.php’);
?>

Error reporting in the server

Sometimes it happens as you might not know which php is your apache pointing as there may be multiple versions. To do the same as you just did in the php.ini file, add the following lines to your httpd.conf to override any and all php.ini files:

php_flag  display_errors        on
php_value error_reporting       2039

This will ensure you that, the above configuration overrides your php.ini file making the defualt value changed. I know your head might poggle up up on the number 2039. Its nothing but, setting up E_ALL & ~E_NOTICE both, if you just want E_ALL alone then go for 2047.

Introducing print statements

Functional bugs don’t generate bugs, its always tricky to get those errors placing print or die statement to juggle out. Sometimes your business logic my syntactically correct, but they are functionality bugs. These are really hard, as these type of errors are not thrown.The die() statement halts program execution and displays text to the Web browser. The die() statement is particularly useful if you don’t want to have to comment out your code, and you only want everything up to the error and the error displayed and nothing after.

 foreach($_GET as $abc => $i){
     print(“What is the data ” . $_GET[$abc "<br>");
 }

Now here, you could notice what is printed in $_GET[$abc]. Now here you can notice whether the values are printed correctly inside a loop making further efficient way using print statement.

In my next part, i will explain you how to play with phpeclipse and built in xdebug for debugging. I hope this small article may come in handy for new comers to PHP world.

Popularity: 1% [?]

About the Author

Vinothbabu has written 23 stories on this site.

In short, my name is Vinoth babu { perra kettala summa authrithala } a UI expert from India, Chennai.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 HTTPguru. All rights reserved.

Our Partners:
WebHosting Jobs| UI expert | Packet Sniffer  | Mobile Home Refinancing  | Laptop LCD screens | CSS Creator| Exchange Hosting| Compare cable companies| Usa online casino | vps | Coupon Codes