Debugging
Techniques in php
White Paper
<Supriya Venepally>
<GE Healthcare (GE-ISU)>
Debugging Techniques in php
Abstract
This document mainly says about the debugging techniques which will be helpful to
the developers during the coding phase and who want to achieve high customer satisfaction for
their works.
By using Different debugging techniques, developer can save precious time by identifying the
bugs easily instead of fighting with the bugs.
These techniques also reduce the repetition of the work for developer and stopping SPR
assignment multiple times from user (client).
Confidential 2
Debugging Techniques in php
About the Author
Supriya is working as Developer since 2008 and has working experience on PHP. She has
worked in different environments like Development, Testing. Presently working in TCS at Kolkata
for the project of GEHC, GE is one of the largest client for TCS.
About the Domain
GE Healthcare is a unit of GE Technology infrastructure, which is a unit of General Electric
(GE). GE Healthcare has a broad range of products and services that include
• Medical imaging ((medicine) the production of visual representations of body parts,
tissues, or organs, for use in clinical diagnosis ;) .
• Information technologies.
• Medical diagnostics (The art or practice of medical diagnosis. Often used in the
plural with a singular verb. A symptom or a distinguishing feature serving as
supporting evidence in a diagnosis. An instrument or a technique used in medical
diagnosis.)
• Patient monitoring systems
• Performance improvement solutions
• Drug discovery.
• Biopharmaceutical manufacturing technologies. These enable healthcare providers
to better diagnose and treat cancer, heart disease, neurological diseases, and other
conditions earlier. The company's vision for the future is to enable a new "early
health" model of care focused on earlier diagnosis, pre-symptomatic disease
detection and disease prevention.
Confidential 3
Debugging Techniques in php
CONTENTS
INTRODUCTION ........................................................................................................................................ 5
ERROR REPORTING ................................................................................................................................ 6
PRINT STATEMENT (ECHO STATEMENT) : ...................................................................................... 6
DIE ():......................................................................................................................................................... 8
APD: ..................................................................................................................................................... 8
CONCLUSION............................................................................................................................................. 8
ACKNOWLEDGEMENTS ......................................................................................................................... 9
REFERENCES ........................................................................................................................................... 10
Confidential 4
Debugging Techniques in php
Introduction
What is meant by debugging?
Debugging is a methodical process of finding and reducing the number of bugs or
defects, in a program
Debugging is, in general, a lengthy and tiresome task. The debugging skill of the programmer is
probably the biggest factor in the ability to debug a problem, but the difficulty of software
debugging varies greatly with the programming language like PHP used and the available
techniques.
We can debug the php code by using different techniques. These debugging techniques will save
the time for developer during the coding phase of the software life cycle model.
This document is going to describe 4 techniques.
1. Error Reporting
2. Print Statement
3. Die ()
4. APD.
Confidential 5
Debugging Techniques in php
Error Reporting
Make Error_reporting = on in [Link] during the configuration.
Error_reporting variable is self evident .it tells PHP whether or not to display errors. The default
value is off. You can discover the current default values of these variables by searching for them
in the [Link] file.
The PHP errors which are normally returned can be quite helpful to a developer who is trying to
debug a script, indicating such things as the function or file that failed, the PHP file it failed in,
and the line number which the failure occurred in.
While debugging the code try to avoid using of show_source (), highlight_string () or
highlight_file ().This can expose hidden values, unchecked syntax, and other dangerous
information. Especially dangerous is running code from known sources with built-in debugging
handlers, or using common debugging techniques. If the attacker can determine what general
technique you are using, they may try to brute-force a page, by sending various common
debugging strings
After completion of coding part make Error reporting = off, because users may get disturb by
seeing error message when the code went to live.
An error message that the PHP interpreter generates falls into one of five different categories:
• Parse error: This Error will occur because of syntax problem. For example if we miss
the semicolon at the end of a statement.
• Fatal error: We can take this Error as high severity one .This Error will happen if
suppose you are including a file in to another file without having defining the first file
• Warning: An advisory from the interpreter that something is fishy in your program, but
the interpreter can keep going. For example Maximum number of seconds need to
execute a query have crossed the time limit.
• Notice: A tip from the PHP interpreter, playing the role of Miss Manners. For example,
printing a variable without first initializing it to some value generates a notice.
Print Statement (echo statement) :
Some times we may not be able to find the errors in the code because
syntactically the code might be correct but functionally it might be wrong. Because of functional
errors we may have false values.
Confidential 6
Debugging Techniques in php
Suppose sql query should return correct values as output but if suppose it is giving wrong values
as output then how can we identify these types of errors?
These are the hardest bugs to find and debug because they throw no errors.
We can’t get to know this type of error until and unless we can see it by using print statement.
Example
Only the Developer knows what need to be displayed and what need not to be displayed.
Suppose sending variable values from one page to another page.
For example [Link] page contains two form variables Group by month, group by quarter as
radio buttons.
Second .php page contains Group by month, Group by rolling quarter as radio buttons.
We need dependency between these two pages like, when we select group by month in [Link]
that selection should be reflected in [Link] file also when we are moving from [Link] to
Second .php.
Suppose in these two pages we are including a file called [Link] and it contains below code
for representing radio buttons.
If ($x= [Link]) //$x is for validation of [Link]
if($month['GROUP_BY'] == 'MONTH') $value = "CHECKED";
if($quarter['GROUP_BY'] == 'QUARTER') $value = "CHECKED";
else
if($month['GROUP_BY'] == 'MONTH') $value = "CHECKED";
if($quarter['GROUP_BY'] == 'QUARTER') $value = "CHECKED";
If the form variable values are not same for both pages we miss the dependency between the
two pages.
For example [Link] contains two form variables Group by month, group by quarter as radio
buttons.
If ($x= [Link])
if($month['GROUP_BY'] == 'MONTH') $value = "CHECKED";
if($quarter['GROUP_BY'] == 'QUARTER') $value = "CHECKED";
[Link] contains Group by month, Group by rolling quarter as radio buttons
Else
if($month['GROUP_BY'] == 'MONTH_SECOND’) $value = "CHECKED";
if($quarter['GROUP_BY'] == 'QUARTER_SECOND') $value = "CHECKED";
In the above example, we have array values in one page as MONTH and another page as
MONTH_SECOND .These two are not matching and hence the dependency in both files is lost.
More over it doesn’t throw an error, does it?
Now we can give print statement to know the different calls made by both the [Link] and
[Link] to [Link]. We should include these print statements in both the php files.
The print statements to be included are as follows,
Confidential 7
Debugging Techniques in php
echo $_REQUEST ['GROUP'];
echo $_REQUEST['GROUP_BY'];
When the above statements are executed we will get to know the different variables that are
being passed and their values as well.
Die ():
The die () method is almost same as the print () method, the only difference is that it will
halt the program execution and display the text to the browser.
APD:
APD is the acronym for Advanced PHP Debugger. It was written to provide profiling and
debugging capabilities for PHP code, as well as to provide the ability to print out a full stack back
trace. APD supports interactive debugging, but by default it writes data to trace files.
By using this built in function we can trace the information of coding. apd_set_pprof_trace ();
We can insert this line any where in the code but while using this be conscious and try to use this
where it is actually required, or else it will create performance issues. It will also result in
unnecessary tracing of the code.
Conclusion
The bad code will through you in critical situation .sometimes releases get postponed if
the severity of the bug is very high, because the developer is not supposed to deliver bug free
code.
By using the above techniques most of the bugs will be known in right time.
Confidential 8
Debugging Techniques in php
Acknowledgements
I would like to extend my sincere thank to all those who have read this paper and
provided me with their valuable inputs and suggestions. Special thanks are due to my all
colleagues for providing me valuable comments and constant encouragement and support during
the course of preparation of this paper.
Confidential 9
Debugging Techniques in php
References
1. [Link]
2. [Link]
3. [Link]/PHP/[Link] - 22k
Hope you find this document quite useful and productive. Feedbacks and critics are appreciated.
You can reach me at [Link]@[Link].
Thanks.
Confidential 10