Skip to content

PHP Debug With Xdebug

Install PHP Xdebug extension

macOS && Linux

PWS provides the ability to quickly install xdebug extensions with just one click

Windows

Currently, it is necessary to manually download xdebug.dll. Click 'Extension' button to open pecl sites and php extension folders

Download the xdebug.dll file at https://pecl.php.net/package/xdebug and put it in the php extension folder

Configure xdebug in php.ini

PWS provides a templates for quick configuration. Just click the copy button and paste the content into the php.ini file

sh
[xdebug]
zend_extension = "xdebug.so"
xdebug.idekey = "PHPSTORM"
xdebug.client_host = localhost
xdebug.client_port = 9003
xdebug.mode = debug
xdebug.profiler_append = 0
xdebug.profiler_output_name = cachegrind.out.%p
xdebug.start_with_request = yes
xdebug.trigger_value=StartProfileForMe
xdebug.output_dir = /tmp

Save and restart PHP service

Configure And User Xdebug In IDE

PhpStorm

Set the PHP version used by PhpStorm

Set the port for Xdebug

Set IDE key and Host

Start Linstening for XDebug connections

Make the breakpoint. Then visit the site. PhpStorm will show this view

Click 'Accept' button. PhpStorm will enter debugging mode

VSCode

There is a popular VSCode PHP extension called php debug. You can find it in the extension window and install it.

After installation, you must reload the VSCode window. Now, again run phpinfo(); method in any PHP file to check if Xdebug is enabled or not.

Now click on the debug console tab and click on add configuration.

Now, you must select the environment which is PHP. VSCode will now add a launch.json file in the root directory.

Finally, add the runtimeExecutable property to the list after port:

shell
"runtimeExecutable": "/usr/local/Cellar/php/8.3.11/bin/php"

Save the launch.json file. Open the debug mode tab, and click on the green debug button to start the debugging option.

You will now see few items in the window, through which you can select what logs Xdebugger will show

Navigate to the Debug Console section which shows you the details of errors and the debug execution buttons on top.

At this point, you can add breakpoints on the lines of code that you need to debug. Note that Xdebug will add the PHP debug script name with the line number on the bottom left section:

You can run the application in the browser, and then read the code line by line, to see the errors and debug them properly. Also, you need to remind a few shortcut functions keys to move through functions and line of codes:

F5: Continue Debugging

F10: Step Over

F11: Step into

Shift + F11: Step out