Prerequisites
To debug PHP code with PhpStorm, we will need Xdebug or Zend Debugger. Make sure either Xdebug or Zend Debugger are installed and configured with PhpStorm.
Starting a Debugging Session from PhpStorm
Let's see how we can start debugging a PHP CLI script from within PhpStorm.
1. Create a Run/Debug Configuration
PhpStorm 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 PhpStorm.
2. Launch the Debugger
Before launching the debugger, make sure a breakpoint is set (or the Run | Break at first line in PHP scripts option is enabled).
From the toolbar, click the "bug" icon to start the debugger. We can also use the Run | Debug menu or the Shift+F9 keyboard shortcut (CMD+F9 on Mac OS X). This will launch our script with the debugger attached, and pauses execution at the first breakpoint.
Starting a Debugging Session from the Command Line
When running CLI scripts one of the requirements below should be met to start a debugging session with IDE:
- Xdebug's remote_autostart setting is enabled
- XDEBUG_CONFIG environment variable exists
Let's go ahead and configure the environment, starting with IDE.
1. Listen for Incoming Debugger Connections
From the toolbar, toggle the “Listen debugger connections” button. We can also use the Run | Start Listening for PHP Debug Connections menu. Enabling this option will ensure PhpStorm reacts when a debugging session is started and opens the debug tool window for us.
Before launching the script, make sure a breakpoint is set (or the Run | Break at first line in PHP scripts menu is enabled).
2. 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. Let's see how we can start our PHP CLI script with debugging enabled using either Xdebug or Zend Debugger.
Optionally: to tell the PhpStorm which path mapping configuration should be used for a connection from certain machine, the value of the PHP_IDE_CONFIG environment variable should be set to serverName=SomeName, where SomeName is the name of the server configured in Settings / Preferences | Languages & Frameworks | PHP | Servers:
If this environment variable is not set - you'll be prompted to specify path mappings manually once IDE detects an incoming XDebug connection.
3. Debug!
Once the script is started, PhpStorm will open the debug tool window and break at the first breakpoint that was set in the script. Next, we can continue debugging our PHP CLI script using PhpStorm.