PhpStorm 2023.3 Help

Configure Xdebug

Download Xdebug

  • Download the Xdebug extension compatible with your PHP version and install it as described in the Xdebug installation guide.

    If you are using a preconfigured AMP (Apache, MySQL, PHP) package, the Xdebug extension may be already installed. Refer to the instructions specific for your package.

Integrate Xdebug with the PHP interpreter

  1. Open the active php.ini file in the editor:

    1. In the Settings dialog (Ctrl+Alt+S) , click PHP.

    2. On the PHP page that opens, click the Browse button next to the CLI Interpreter field.

    3. 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.

  2. To disable the Zend Debugger and Zend Optimizer tools, which block Xdebug, remove or comment out the following lines in the php.ini file:

    zend_extension=<path_to_zend_debugger> zend_extension=<path_to_zend_optimizer>
  3. To enable Xdebug, locate or create the [xdebug] section in the php.ini file and update it as follows:

    [xdebug] zend_extension="<path to xdebug extension>" xdebug.mode=debug xdebug.client_host=127.0.0.1 xdebug.client_port="<the port (9003 by default) to which Xdebug connects>"
    [xdebug] zend_extension="<path to xdebug extension>" xdebug.remote_enable=1 xdebug.remote_host=127.0.0.1 xdebug.remote_port="<the port (9000 by default) to which Xdebug connects>"

    To enable multi-user debugging via Xdebug proxies, locate the xdebug.idekey setting and assign it a value of your choice. This value will be used to register your IDE on Xdebug proxy servers.

  4. Save and close the php.ini file.

  5. Verify Xdebug installation by doing any of the following:

    • In the command line, run the following command:

      php --version

      The output should list Xdebug among the installed extensions:

      Xdebug extension installed
    • Create a php file containing the following code:

      <?php phpinfo();

      Open the file in the browser. The phpinfo output should contain the Xdebug section:

      Xdebug 3 support enabled
      Xdebug support enabled

Configure Xdebug in PhpStorm

Check Xdebug installation

  1. Press Ctrl+Alt+S to open the IDE settings and then select PHP.

  2. Check the Xdebug installation associated with the selected PHP interpreter:

    1. On the PHP page, choose the relevant PHP installation from the CLI Interpreter list and click the Browse button next to the field. The list shows all the PHP installations available in PhpStorm, see Configure local PHP interpreters and Configure remote PHP interpreters.

    2. The CLI Interpreters dialog that opens shows the following:

      • The version of the selected PHP installation.

      • The name and version of the debugging engine associated with the selected PHP installation (Xdebug or Zend Debugger). If no debugger is configured, PhpStorm shows the corresponding message:

        ps_interpreters_debugger_not_installed.png

    Alternatively, open the Installation Wizard, paste the output of the phpinfo(), and click Analyze my phpinfo() output. Learn more about checking the Xdebug installation in Validate the configuration of a debugging engine.

Define Xdebug behaviour

  1. In the IDE settings (Ctrl+Alt+S), select Debug under the PHP node to open the Debug page.

  2. In the Xdebug area, specify the following settings:

    • Debug port: appoint the port through which the tool will communicate with PhpStorm.

      This must be the same port number as specified in the php.ini file:

      xdebug.client_port="<the port (9003 by default) to which Xdebug connects>"
      xdebug.remote_port="<the port (9000 by default) to which Xdebug connects>"

      By default, Xdebug 2 listens on port 9000. For Xdebug 3, the default port has changed from 9000 to 9003. You can specify several ports by separating them with a comma. By default, the Debug port value is set to 9003,9000 to have PhpStorm listen on both ports simultaneously.

      To have PhpStorm accept any incoming connections from Xdebug engine through the port specified in the Debug port field, select the Can accept external connections checkbox.

    • Resolve breakpoint if it's not available on the current line (Xdebug 2.8+): the selected checkbox enables support for the Xdebug breakpoints resolving mechanism. Under this mechanism, the debugger evaluates whether PHP can generate internal executable bytecode for the current line. If there is no such code on the line that the breakpoint refers to, the corresponding breakpoint cannot be hit. Xdebug will scan up to 5 subsequent lines, stop at the line where executable code is located, and update the breakpoint definition to this line.

    • Force break at first line when no path mapping specified: the selected checkbox makes the debugger stop as soon as it reaches and opens a file that is not mapped to any file in the project on the Servers page. The debugger stops at the first line of this file and Examine/update variables shows the following error message: Cannot find a local copy of the file on server <path to the file on the server> and a link Click to set up mappings. Click the link to open the Resolve Path Mappings Problem dialog and map the problem file to its local copy.

      When this checkbox is cleared, the debugger does not stop upon reaching and opening an unmapped file, the file is just processed, and no error messages are displayed.

    • Move breakpoint to resolved position if it's different from the source: the selected checkbox enables automated adjustment of the breakpoint position to the line where Xdebug actually stops after resolving the breakpoint.

      Note that the feature works only when the Resolve breakpoint if it's not available on the current line (Xdebug 2.8+) checkbox is enabled.

    • Force break at first line when a script is outside the project: the selected checkbox makes the debugger stop at the first line as soon as it reaches and opens a file outside the current project. With this checkbox cleared, the debugger continues upon opening a file outside the current project.

    • Enable return function value debugging (Xdebug 3.2+): the selected checkbox adds an extra debugging step for inspection of return values in functions that return them immediately without storing in intermediate variables.

  3. In the External connections area, specify how you want PhpStorm to treat connections received from hosts and through ports that are not registered as deployment server configurations.

    • Ignore external connections through unregistered server configurations: select this checkbox to have PhpStorm ignore connections received from hosts and through ports that are not registered as deployment server configurations. When this checkbox is selected, PhpStorm does not attempt to create a deployment server configuration automatically.

    • Break at first line in PHP scripts: select this checkbox to have the debugger stop as soon as connection between it and PhpStorm is established (instead of running automatically until the first breakpoint is reached). Alternatively turn on the Run | Break at first line in PHP scripts option from the main menu.

    • Max. simultaneous connections: use this spin box to limit the number of external connections that can be processed simultaneously.

Configure Xdebug for using in the On-Demand mode

PhpStorm supports the On-Demand mode, where you can disable Xdebug for your global PHP installation and have it enabled automatically on demand only when you are debugging your command-line scripts or when you need code coverage reports. This lets your command line scripts (including Composer and unit tests) run much faster.

  1. Disable Xdebug for command-line scripts:

    1. In the Settings dialog (Ctrl+Alt+S) , go to PHP.

    2. From the PHP executable list, choose the relevant PHP interpreter and click the Browse button next to it. In the CLI Interpreters dialog that opens, click the Open in Editor link next to the Configuration file: <path to php.ini> file. Close all the dialogs and switch to the tab where the php.ini file is opened.

    3. In the php.ini file, find the [xdebug] section and comment the following line in it by adding ; in preposition:

      ;[xdebug] ;zend_extension = "<path to xdebug extension>"
    4. Open the CLI Interpreters dialog and click the Reload button next to the PHP executable field. PhpStorm informs you that debugger is not installed:

      ps_interpreters_debugger_not_installed.png
  2. To enable PhpStorm to activate Xdebug when it is necessary, specify the path to it in the Debugger extension field, in the Additional area. Type the path manually or click the Browse button and select the location in the dialog that opens.

Configure Xdebug for using in the Just-In-Time mode

PhpStorm supports the use of Xdebug in the Just-In-Time (JIT) mode so it is not attached to your code all the time but connects to PhpStorm only when an error occurs or an exception is thrown. Depending on the Xdebug version used, this operation mode is toggled through the following settings:

The mode is available both for debugging command-line scripts and for web server debugging.

Depending on whether you are going to debug command-line scripts or use a Web server, use one of the scenarios below.

Command-line scripts

For debugging command-line scripts, specify the custom -dxdebug.remote_mode=jit (for Xdebug 2) or -dxdebug.start_upon_error=yes (for Xdebug 3) directive as an additional configuration option:

  1. Press Ctrl+Alt+S to open the IDE settings and then select PHP.

  2. From the PHP executable list, choose the relevant PHP interpreter and click the Browse button next to it.

  3. In the CLI Interpreters dialog that opens, click the Browse button next to the Configuration options field in the Additional area.

  4. In the Configuration Options dialog that opens, click the Add button to add a new entry.

    Type xdebug.start_upon_error in the Configuration directive field and yes in the Value field.

    Type xdebug.remote_mode in the Configuration directive field and jit in the Value field.

    When you click OK, you return to the CLI Interpreters dialog where the Configuration options field shows -dxdebug.remote_mode=jit (for Xdebug 2) or -dxdebug.start_upon_error=yes (for Xdebug 3).

Web server debugging

  1. Make sure that the debugging engine on your local or remote web server is properly configured. For more information, refer to Validate the configuration of a debugging engine.

  2. Open the php.ini file which is reported as loaded and associated with Xdebug.

  3. In the php.ini file, find the [xdebug] section.

    Change the value of the xdebug.start_upon_error from the default default to yes.

    Change the value of the xdebug.remote_mode from the default req to jit.

See also Just-In-Time debugging and PHP Exception Breakpoints with PhpStorm and Xdebug

Configure Xdebug running in a Docker container

To configure Xdebug running in a Docker container, provide the Xdebug-specific parameters in the Dockerfile, for example:

RUN pecl install xdebug \ && docker-php-ext-enable xdebug && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

In this example, we're modifying /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini providing the mode and client_host Xdebug parameters.

Note that the xdebug.client_host value should be replaced with the IP address of the machine where PhpStorm is running, which is accessible from the Docker container. If you are using Docker for Windows or Docker for Mac, you can set xdebug.client_host to host.docker.internal, which automatically resolves to the internal address of the host, letting you easily connect to it from the container.

RUN pecl install xdebug \ && docker-php-ext-enable xdebug && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ && echo "xdebug.remote_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

In this example, we're modifying /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini providing the remote_enable and remote_host Xdebug parameters.

Note that the xdebug.remote_host value should be replaced with the IP address of the machine where PhpStorm is running, which is accessible from the Docker container. If you are using Docker for Windows or Docker for Mac, you can set xdebug.remote_host to host.docker.internal, which automatically resolves to the internal address of the host, letting you easily connect to it from the container.

Configure Xdebug running on a Vagrant instance

To configure Xdebug running on a Vagrant instance, connect to the Vagrant machine and provide the Xdebug-specific parameters in the php.ini file:

[xdebug] zend_extension="<path to xdebug extension>" xdebug.mode=debug xdebug.client_host=10.0.2.2 xdebug.client_port=9003

Note that the xdebug.client_host value is 10.0.2.2. This is the gateway used in the default Vagrant setup, which allows connecting from the instance to host where PhpStorm is running.

[xdebug] zend_extension="<path to xdebug extension>" xdebug.remote_enable=1 xdebug.remote_host=10.0.2.2 xdebug.remote_port=9000

Note that the xdebug.remote_host value is 10.0.2.2. This is the gateway used in the default Vagrant setup, which allows connecting from the instance to host where PhpStorm is running.

Last modified: 25 March 2024