Xdebug is a PHP extension which provides debugging, profiling, code coverage, stack traces and many other capabilities. In this succinct tutorial, I will show us how to set up Xdebug that ships with XAMPP in PhpStorm. See the steps below. Open up php.ini (located at C:path-to-xamppphp) for editing. For example index.php; After that edit the configuration: Choose a PHP Web Page (On old PHPStorm version it might be called PHP Web Application) on the drop down after you click the green plus. Edit the Configuration Name and add new server. Add new server with Host = localhost, on port 80 with Xdebug. Release date: 2021-04-08. Linux, macOS: source. Windows binaries: PHP 7.2 VC15 (64 bit) PHP 7.2 VC15 (32 bit) PHP 7.2 VC15 TS (64 bit). 修改PHP配置文件 php.ini Xdebug zendextension=C:webserverserverphp-5.6.9extphpxdebug-2.3.3-5.6-vc11-x8664.dll;允许远程IDE调试 xdebug.remoteenable=on;远程IDE所在HOST和PORT xdebug.remotehost=localhost xdebug.remoteport=9000 xdebug.remotehandler=dbgp;开启远程调试自动启动 xdebug.remoteautostart = Off;可以是任意Key,这里设定为PHPSTORM xdebug.
- Install Xdebug Phpstorm Ubuntu
- Phpstorm Xdebug Vagrant
- Php Xdebug Phpstorm Docker
- Php Docker Xdebug Phpstorm
Besides debugging the entire application, you can debug separate HTTP Requests. This is helpful when you are actually interested in a specific page that is accessed in a number of steps, but for this or that reason you cannot specify this page as the start page for debugging, for example, because you need to 'come' to this page with certain data.
To debug PHP HTTP requests in PhpStorm, you can use the following methods:
Compose and debug the request via the HTTP client in the code editor, which is the recommended approach.
Use the PHP HTTP Request run configuration. Based on the configuration settings, PhpStorm composes the request to run.
Prepare the debugging engine
Before you start debugging, make sure that you have a debugging engine installed and configured properly. PhpStorm supports debugging with two most popular tools: Xdebug and Zend Debugger. These tools cannot be used simultaneously because they block each other. To avoid this problem, you need to update the corresponding sections in the php.ini file as described in Configure Xdebug and Configure Zend Debugger.
Open the active php.ini file in the editor:
In the Settings/Preferences dialog Ctrl+Alt+S, click PHP.
On the PHP page that opens, click next to the CLI Interpreter field.
In the CLI Interpreters dialog that opens, the Configuration file read-only field shows the path to the active php.ini file. Click Open in Editor.
Set the breakpoints
Breakpoints are source code markers used to trigger actions during a debugging session. Typically, the purpose behind setting a breakpoint is to suspend program execution to allow you to examine program data. However, PhpStorm can use breakpoints as triggers for a variety of different actions. Breakpoints can be set at any time during the debugging process. Your breakpoints don't affect your source files directly, but the breakpoints and their settings are saved with your PhpStorm project so you can reuse them across debugging sessions.
Place the caret at the desired line of the source code.
Breakpoints can be set in the PHP context inside php, html, and files of other types. Line breakpoints can be set only on executable lines, but not on comments, declarations, or empty lines.
Do one of the following:
Click the gutter area at a line where you want to toggle a breakpoint.
From the main menu, choose Run | Toggle Line Breakpoint.
Press Ctrl+F8.
Debug the request via the HTTP client in the code editor
Using the built-in HTTP Client, you can compose, execute, and debug HTTP requests directly from the PhpStorm code editor.
Open an existing HTTP request file, or create a new one: in the File menu, point to New, and then click HTTP Request.
Compose an HTTP request for the query that you need to debug.
Position the caret at the request and press Alt+Enter or click in the editor gutter. From the popup menu, select PHP Debug <host>.
If you have environments defined, select PHP Debug with ... and choose the environment in the popup menu. The selected environment will be used as the default one when executing or debugging the request later.
PhpStorm will automatically add the
XDEBUG_SESSION
cookie to the request, execute it, and stop at the specified breakpoint.
When a request is executed, PhpStorm automatically creates a dedicated temporary HTTP Request run/debug configuration for it. You can save it as a permanent run/debug configuration if necessary.
Press Shift+F10 to run or Shift+F9 to debug the corresponding saved run/debug configuration at any moment without the need to open the request file in the editor. This can be useful if you are working on the web service endpoint implementation in a .php file and want to quickly test it by sending an HTTP request.
Create a debug configuration of the type PHP HTTP Request
PhpStorm comprises the settings specified in this configuration into a PHP HTTP request. Note that using HTTP Client in editor for debugging HTTP requests is a more convenient and recommended approach.
Open the Run/Debug Configuration dialog by doing one of the following:
From the main menu, choose Run | Edit Configurations.
Press Alt+Shift+F10, then press 0 to display the Edit Configuration dialog or select the configuration from the popup and press F4.
Click on the toolbar or press Insert. From the list, select the PHP HTTP Request configuration type. The PHP HTTP Request dialog opens.
Specify the configuration name.
In the Server list, specify the debug server configuration to interact with the Web server where the application is executed. Select one of the existing configurations or click Browse and define a debug server configuration in the Servers dialog that opens as described in Create a PHP debug server configuration.
In the URL field, complete the
host
element of the request to debug. Type the path relative to the host specified in the debug server configuration. As you type, PhpStorm composes the URL address on-the-fly and displays it below the field.Specify whether you want to bring any data to the target page. From the Request method list, choose the relevant request type:
To access the page without bringing any data, choose GET.
To access the page with some data saved in variables, choose POST and type the relevant variables in the Request body field.
By default, the Project Encoding is used in requests' encoding if it is not specified explicitly, for example:
header('Content-type: text/html;charset=utf-8');The Project Encoding is specified on the File Encodings page of the Settings/Preferences dialog Ctrl+Alt+S.
In the Query field, type the query string of the request. This string will be appended to the request after the ? symbol.
Click OK, when ready.
Initiate a debugging session and examine the suspended program
To start debugging, click the Debug button on the toolbar.
As soon as the debugger suspends on reaching the first breakpoint, examine the application by analyzing frames. A frame corresponds to an active method or function call and stores the local variables of the called method or function, the arguments to it, and the code context that enables expression evaluation. All currently active frames are displayed on the Frames pane of the Debug tool window, where you can switch between them and analyze the information stored therein in the Variables and Watches panes. For more details, see the section Examining a Suspended Program.
Continue running the program and examine its frames as soon as it is suspended again.
To control the program execution manually, step through the code using the commands under the Run menu or toolbar buttons: Step IntoF7, Step OutShift+F8, Step OverF8, and others. For more details, see Step through the program.
To have the program run automatically up to the next breakpoint, resume the session by choosing Run | Debugging Actions | Resume Program or pressing F9.
August 11, 2016Yann Jacquot6 min read
Warning: this article concerns php5 version. If your PHP version is different, replace php5 by php/X.Y in paths (X.Y is your PHP version) and phpX.Y in command. For example :
sudo apt-get install php5-xdebug
becomessudo apt-get install php5.6-xdebug
/etc/php5/mods-available/xdebug.ini
becomes/etc/php/5.6/mods-available/xdebug.ini
I love debuggers. They allow me to understand deep down in my code why something doesn't work, and are even more useful when I work on legacy projects.
When I work on the client side, the browser already provides me all the tools I need to dig deeper than some console.log()
scattered semi-randomly in the source code.
However, I struggled to configure my workspace when I worked on a PHP Symfony2 project hosted in a Vagrant virtual machine.
Step1: Install Xdebug on your Vagrant virtual machine
That may seem obvious, but you need to have Xdebug installed on your virtual machine to benefit from its services.
Then, configure it:
with the following lines:
Finally, if you use php5-fpm, you need to restart it:
If you use Ansible to provision your virtual machine, you can also use a ready-to-action Xdebug role.
Step2: Configure PhpStorm
First, select the 'Edit configurations' item in the 'Run' menu.
Then, add a new 'PHP Remote Debug' configuration.
We will use the IDE key configured in your Vagrant and in your browser.
To fully configure this debugger configuration, you will need to create what PhpStorm calls a server.
- Fill the correct hostname
- Check 'Use path mappings' checkbox, and write the project's absolute path
on your Vagrant virtual machine
Step3: Configure Xdebug
Use Xdebug to debug your web application on Chrome
Now that Vagrant with Xdebug is up and running, let's configure Xdebug Chrome extension.
First, we need to install it from Chrome Web Store
Make sure that the extension is enabled on your browser's extensions list page.
Now, you should see on the right side of the address bar the extension's symbol.
Right-click on it, then click on the 'Options' sub-menu.
You have to use the IDE key previously set.
Xdebug plugin also exists for other browsers.
Install Xdebug Phpstorm Ubuntu
Finally, in your browser click on the bug in your address bar to switch to the 'Debug' mode
Use Xdebug to debug your APIs route with Postman
Phpstorm Xdebug Vagrant
Once your Xdebug configuration is added, you need to add ?XDEBUG_SESSION_START=_<XDEBUG_KEYNAME>_
at the end of your route. And that's all!
Use Xdebug to debug commands or unit tests
To use Xdebug for debugging commands or unit tests, first, you need to add xdebug.remote_autostart=true
in XDebug configuration file of your Vagrant xdebug.ini.
Then, you need to specify the xdebug.remote_host
(IP address of your local from your Vagrant) when launching the command from the virtual machine’s terminal.
- First, get the host IP address by using
ifconfig
from your local terminal (the host) :
- Then, launch your command
For instance, if you want to debug your unit tests in a Symfony project, you can run:
Step4: Enjoy!
Now, in PhpStorm you:
- Add your breakpoints by clicking to the left of the lines
- Click on the bug icon on the upper-right corner
You should now be able to break on the exact line you selected in your IDE.
Bonus: Performance
You need to know that enabling Xdebug slows down your app (x2), the fastest way to disable it is to run the following command: php5dismod xdebug
Use php5enmod xdebug
to enable it back. Each time you'll also need to restart php-fpm or apache.
Troubleshooting: I did the tutorial but Xdebug doesn’t work
It’s probably because a symbolic link is missing in your php conf.
- First type in your virtual machine
php -i | grep xdebug
If you have no response, it means Xdebug is not set correctly
Php Xdebug Phpstorm Docker
- Then check if Xdebug is mentioned when running
php -v
from your Vagrant. - If not, add a symbolic link to specify that the Xdebug module should be enabled, for instance (you may need sudo):
- You should obtain by running
php -v
Php Docker Xdebug Phpstorm
I hope you've made your way through the process and that it will improve your efficiency as much as it does for me. If you have other ways to configure your debugger, feel free to share them in the comments section below.