Category Archives: Error Debugging Technique In PHP

Best php error debugging technique

Debugging PHP code is a nightmare for all Php developers and these are the times when they miss the thread functionality like java in php, However some simple php debugging techniques can help y0u to code faster and thus save very valuable coding time.

There are many PHP debugging techniques that can save you countless hours when coding. An effective but basic debugging technique is to simply turn on error reporting. Another slightly more advanced technique involves using print statements, which can help pinpoint more elusive bugs by displaying what is actually going onto the screen.
So , My debugging tips are (in no particular order):-

1) Live PHP debugging:- Use a editor with inbuilt live Php debug (Like phped ,Phpdesigner, eclipse etc), Editors like editpad, notepad doesn’t 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.

Small human errors like missing a semicolon , not terminating the line correctly, missing brackets etc can be easily found using a live debug php editor.

2) Use Xdebug:-The Xdebug extension allows you to find stack traces and function traces in error messages, memory allocation and protect from infinite recursions happening

It also provides profiling information for PHP code and is having the capability to debug scripts interactively with a debug client.

Xdebug can be found at xdebug.org (Editors like Phpdesigner and phpdebug already contain Xdebug inbuilt )

3) Activate Php error messages:- php.ini contains a lot of configuration options , Among those options there are a couple of options which control the way error messages are displayed to a user. Make sure that following two options are set as follows:-

display_errors = On

error_reporting = E_ALL & ~E_NOTICE

4) Use print and echo statements beforehand at critical points for debug:- This is somewhat a homegrown method that i use for php debugging which i found extremely helpful and useful in debugging big php codes.

While coding i create some virtual sections of code and in these virtual sections i put the echo statement to ensure that the code is passing through that portion correctly and echoing the data correctly that’s required.

But since i can’t left them always on , I put up a debug_check if statement to check whether the code is working in debug mode or production mode.

For example

< ?php $debug_check = 1; foreach ($array as $data) { if ($debug_check == 1) print ($data); } ?>

Here the $debug_check variable is defined at the top of the web script. Once this is changed to 1, it displays all echo and print statements , This is when i am debugging the code.

When i am in production mode i simply change the value of $debug_check to 0 which again hide all echo and print statements, This takes a little more time in coding initally , but proves to very helpful in long run.

5) Use Frameworks:- Frameworks are one of the most important change that is implemented in modern programming , Php frameworks like codeignitor and cakephp provides a lot of functionality for setting up test cases, units and debugging.

6. Debugging PHP is About var_dump

If you’re looking for php debugging techniques, i have to say that var_dump is most times the way to go about it. This command is all you need to echo php information. There shouldn’t really be many cases where you need anything more than dumping values in PHP, in order to debug your code.
Also, Since most of the code is ran via libraries the error messages are nicely crafted and provides inbuilt details.

6. When You Absolutely Need Global Values, Create a Config File

It is a bad practice to create global values for everything. There are limited cases where you would actually need to do so. Doing it for database tables or database connection information is a good idea, but do not use global variables throughout your PHP code. Moreover, it is always a better idea to keep your global variables at a single config.php file.


Some Important Notes to Care With
:

Missing Semicolons – Every PHP statement ends with a semicolon (;). PHP doesn’t stop reading a statement until it reaches a semicolon. If you leave out the semicolon at the end of a line, PHP continues reading the statement on the following line.

Not Enough Equal Signs – When you ask whether two values are equal in a comparison statement, you need two equal signs (==). Using one equal sign is a common mistake.

Misspelled Variable Names – If you misspelled a variable then PHP understands it as a new variable. Remember: To PHP, $test is not the same variable as $Test.

Missing Dollar Signs – A missing dollar sign in a variable name is really hard to see, but at least it usually results in an error message so that you know where to look for the problem.

Troubling Quotes – You can have too many, too few, or the wrong kind of quotes. So check for a balanced number of quotes.

Missing Parentheses and curly brackets – They should always be in pairs. Array Index – All the arrays should start from zero instead of 1.

Moreoever, handle all the errors properly and direct all trace messages into system log file so that if any problem happens then it will be logged into system log file and you will be able to debug that problem.