There are two camps when it comes to debugging PHP applications. One one side, we have the "echo tribe" relying on echo
and var_dump
for debugging output, while on the other side, the "IDE-wizards" leverage IDE's with Xdebug.
Now, Xdebug 3.1. introduced a new tool in town which might appeal to both camps – xdebug_notify
. It allows developers to send message or variable to IDE, presenting the information in a clear manner. In case of PHP Tools for VS Code, the output is directed to the Debug Console.
In the realm of PHP debugging, developers face distinct challenges depending on their chosen debugging method. The "echo tribe," using echo
and var_dump
, might grapple with code clutter and limited insights, particularly if their code base expands. Conversly, "IDE-wizards", enjoying the perks of breakpoints and step-by-step debugging might find themselves bound to interruption-based methods.
1. Effortless Integration:
Easily integrate xdebug_notify
into your existing workflow without the need for a complete shift in debugging methodology. It's a seamless addition that enhances your traditional approach.
2. Streamlined Debugging Output:
Bid farewell to the clutter associated with extensive echo
and var_dump
statements. With xdebug_notify
, selectively output variables into VSCodes Debug Console, creating a cleaner and more focused debugging experience.
1. Non-Intrusive Inspection:
Enjoy non-intrusive variable inspection without the need for explicit breakpoints. With xdebug_notify
, selectively output variables to your IDE's console, offering a more dynamic and flexible debugging experience.
2. Customized Variable Output:
Tailor your debugging approach by choosing which variables to inspect and when. The power of xdebug_notify
lies in its ability to deliver specific information based on your conditions and preferences. E.g. Be incorporating xdebug_notify
to code you can find yourself reaching for conditional breakpoints much less.
To use xdebug_notify
, follow these steps:
1.) Install Xdebug
at least 3.1.0
(Win, Mac, Linux)
2.) Install PHP Tools for VS Code if you haven't already.
In VSCode, open a new folder and add index.php
file, and copy the following code:
<?php
xdebug_notify("script begins");
$x = array("a", "b", "c");
xdebug_notify($x);
xdebug_notify('script ends');
You will notice xdebug_notify
is not recognized. This is because xdebug functions are not by default included. If you have PHP Tools PREMIUM, you can hover over the functions click Add "xdebug" to "php.stubs" settings
. If you don't, just alter your .vscode/settings.json
manually.
This adds xdebug stub to .vscode/settings.json
, allowing IDE to recognize Xdebug functions
{
"php.stubs": [
"*",
"xdebug"
]
}
As launch.json
is not configured, you will get following dialog. Choose Launch built-in server and Debug
Check the Debug Console for outputs of your xdebug_notify
calls. They will be mixed with other relevant debugging information.
If the debugging session is still alive, expand the array, by clicking on the >
.
Note: The Debug Console relies on an active debugging session to display variable information. If you encounter the message "No debugger available, can not send 'variables'" after the debugging session has ended, it is expected behavior. Ensure your debugging session is active to expand and inspect variables in the Debug Console.
Regardless your favorite way of debugging, xdebug_notify
can prove to be valuable tool. Give it a try and let us know if there are ways how to further enhance your PHP debugging experience.