Debug a PHP CLI script
PHP applications are not always web applications. Various command line tools, daemons, message queue processing applications and other types of applications typically run in the PHP CLI. There are several ways to start a PHP CLI debugging session. You can start it from within IntelliJ IDEA and make it start the script and attach the debugger to it. Alternatively, you can let IntelliJ IDEA listen for incoming debugger connections and start the script outside the IDE. We'll take a look at both options.
Before you start debugging, make sure that you have a debugging engine installed and configured properly. IntelliJ IDEA 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 under Languages & Frameworks.
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.
Start a debugging session from IntelliJ IDEA
To start debugging a PHP CLI script from within IntelliJ IDEA, perform the following steps.
Create a Run/Debug Configuration
IntelliJ IDEA uses Run/Debug configurations to execute a script from within the IDE. A configuration can define additional arguments for the PHP interpreter as well as launch other commands prior to starting our script. We will need a Run/Debug configuration to start the debugger from within IntelliJ IDEA.
Create a Run/Debug configuration for a PHP script manually
Create a new Run/Debug configuration using themenu.
Add a new configuration of the PHP Script type and provide the required parameters, such as the script to be executed.
Save the created Run/Debug configuration.
Generate a Run/Debug configuration for a PHP script
Right-click in the Project tool window, and select from the context menu (make sure to pick the item marked with ). Alternatively, open the script in the editor, press Alt+Shift+F9, and select the script to be debugged.
The IDE will launch the script with the debugger enabled, and open the Debug tool window.
Launch the Debugger
Click on the IntelliJ IDEA toolbar.
Selectfrom the main menu.
Switch between configured PHP interpreters on the fly
Press Ctrl+Shift+A and start typing
Change PHP interpreter. In the suggestions list, select the Change PHP interpreter action.
If necessary, you can assign a keyboard shortcut for this action either directly in the suggestions list by pressing Alt+Enter, or at a later point as described in Configuring keyboard shortcuts.
The selected interpreter will be set as the default project interpreter on the PHP page of the Settings/Preferences dialog Ctrl+Alt+S . This will also affect the run/debug configurations, test frameworks', and quality tools' configurations that are set to use the default project interpreter.
Start a debugging session from the command line
Before you start a debugging session with IntelliJ IDEA when running CLI scripts, make sure that any of the following requirements is met:
remote_autostartoption is enabled.
XDEBUG_CONFIGenvironment variable exists.
Listening for incoming debugger connections
In IntelliJ IDEA, enable listening to incoming debug connections by either clicking on the toolbar or selecting debug tool window automatically. Before launching the script, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings/Preferences dialog Ctrl+Alt+S.. This will ensure IntelliJ IDEA reacts when a debugging session is started and opens the
Start the script with debugger options
Since we'll be starting the script from the command line, we will have to make sure it is started with the required settings to enable the debugger.
Starting the script with Xdebug
Xdebug has various configuration options which we can use to let the PHP interpreter reach out to IntelliJ IDEA. These parameters have to be passed to the PHP interpreter using the
-d command line parameter. Alternatively, you can set an environment variable so that you don't need to provide the
-d parameters every time.
Start the script with debugging using PHP command line switches
Launch PHP with the following command-line options:php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_connect_back=0 path/to/script.php
You may use 10.0.2.2 instead of 127.0.0.1 with Vagrant (see related SO question).
Start the script with debugging using an environment variable
Set an environment variable that configures Xdebug:set XDEBUG_CONFIG=remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"
Start the script normally:php path/to/script.php
Optionally, we can use Xdebug's remote_autostart setting to always start a debugging session for every script that is run.
Starting the script with Zend Debugger
Zend Debugger has various configuration options which we can use to let the PHP interpreter reach out to IntelliJ IDEA. These parameters have to be passed to the PHP interpreter using an environment variable.
Start the script with debugging
QUERY_STRINGenvironment variable:set QUERY_STRING=start_debug=1&debug_host=127.0.0.1&no_remote=1&debug_port=10137&debug_stop=1export QUERY_STRING="start_debug=1&debug_host=127.0.0.1&no_remote=1&debug_port=10137&debug_stop=1"
Start the script normally:php path/to/script.php
Optionally, to tell IntelliJ IDEA which path mapping configuration should be used for a connection from a certain machine, the value of the
PHP_IDE_CONFIG environment variable should be set to
SomeName is the name of the server configured on the of the Settings/Preferences dialog Ctrl+Alt+S.
If this environment variable is not set - you'll be prompted to specify path mappings manually once IDE detects an incoming Xdebug connection.
Once the script is started, IntelliJ IDEA will open the Debug tool window and break at the first breakpoint that was set in the script. You can now continue debugging a PHP CLI script as described in Examine suspended program.
If the script that is being debugged is not a part of the project that's open in IntelliJ IDEA, the IDE will still open the script in the editor and pause execution at the first statement. This makes it possible to quickly debug any PHP CLI script, even if we don't have a IntelliJ IDEA project for it yet.