Every program no matter how well written can have bugs or other defects.
Using a PHP Debugger will help find and eliminate unwanted software bugs from your PHP programs.
Using a PHP Debugger to Debug a PHP Script
For the beginner PHP developer, PHP debugging typically involves the use of print statements or logging of
error messages from within in your PHP application code, for example, printing the value of different variables
throughout different stages of the program execution.
This approach works in simple cases, but suffers from many drawbacks, for example:
- It requires adding more code into your application, which takes time and might introduce even more bugs into your PHP script.
- Debugging of PHP with print statements it's highly repetitive -- you'll need to keep printing out the value of different variables even if they don't change, and you'll have to clean up the mess afterwards.
- Introduction of so many print statements and conditional can affect the performance of your PHP application.
A PHP Debugger provides a powerful
way to simplify PHP debugging because it gives you complete visibility
and control over the execution of PHP scripts and doesn't require that
you make any changes to your PHP code. For example, using
PhpED's PHP Debugger from NuSphere, you can perform various PHP debugging
- Inspect any PHP variable or class
- Change the value of a PHP variable
- Set break points to interrupt PHP script execution
- Make these break points act based on various conditions
- Call a PHP function on the fly and inspect the results
- Set break points dependant on Global True Expression
- Jump to a different line of PHP code
- Unwind the stack to see where an error originated
- Support for remote PHP debugging
- Enable secure debugging with SSH
You can download a free evaluation of PhpED's PHP Debugger so that you can follow along with the rest of this tutorial.
How to use the PhpED PHP Debugger
PhpED's PHP Debugger will
let you step through your PHP code, control the program execution flow,
watch the values of variables in the script as well as contents of the
arrays and classes at each stage. You can even assign different values
to the variables on the fly and do all of it without making any changes
to your PHP application. In the following tutorial, you will learn how
to use PhpED's PHP Debugger to find and eliminate bugs.
Step Through Lines of PHP Code
PhpED's PHP Debugger lets
you step through the execution of PHP scripts, line-by-line, just as
you would using a conventional software debugger. Using the PhpED Debugger
toolbar, you can:
- Step-In: Execute the following line of PHP code. If the next line of code is a function call, then begin execution at the beginning of the function.
- Step-Out: Execute program execution until you reach the line of PHP code that called the currently executing function.
- Step-Over: Execute the current line of PHP code
Single-Stepping through a PHP script using a PHP debugger is the easiest way to isolate bugs and is illustrated here:
Inspecting PHP Variables or Classes
PhpED's PHP Debugger provides
three ways to see the values of PHP variables or the members of the
- The value of a variable is shown when the cursor is hovered over the variable in PHP code
- The values of the Local variables and classes are displayed in Locals tab of PhpED debugger
- The values of the Global variables and classes are displayed in Globals tab of PhpED debugger
- Variables and classes added to the Watch tab of PhpED debugger and displayed there
The content of Locals, Globals and Watch tabs changes as you step through the code.
Assign a different value to PHP variables in the script
The PhpED PHP Debugger will even let you assign
a different value to any PHP variable, so you can your fix before you
change the actual PHP code in the script.
Set/toggle break points to interrupt PHP script execution
PhpED allows you to set and toggle breakpoints
anywhere in your PHP code (illustrated below). The lines of code highlighted
in blue represent breakpoints. The line in red shows the PHP script
current state. You can set/toggle breakpoints from a toolbar, by typing
the "F5" key, or by clicking on the red dot to the left of
the line of code.
Call PHP function on the fly and inspect the results
Using the Immediate tab in
the PhpED PHP debugger lets you can call any
PHP function on the fly, even in the middle of the execution of your
PHP script. Simply type in the function call in the window and press
Enter. The functionality of immediate tab is illustrated below:
In this tutorial you learned
about the different utilities to find and eliminate bugs from your PHP
scripts using the PHP Debugger in NuSphere PhpED.