PHP Tools VSCode Debug

PHP Debugger on VSCode

by Daniel Loureiro on 2 min read

This tutorial shows how to debug PHP on VSCode Editor.

# 1. Install xdebug

sudo apt install php-xdebug

# 2. Check installation

Create a temporary file to display your phpinfo information:

echo "<?php phpinfo();" > phpinfo.php
php -S localhost:3000
    PHP Development Server started at Wed Jun  5 17:20:29 2019
    Listening on http://localhost:3000
    Document root is /home/daniel
    Press Ctrl-C to quit.

Now, (1) open the page, (2) search for xdebug, and (3) get the xdebug.ini path: PHPInfo on browser

Down on the same page, check if the module is enabled: xdebug details on phpinfo page

Now you can remove the phpinfo.php file (optional):

rm phpinfo.php

# 3. Add these lines to xdebug.ini

# /etc/php/7.3/cli/conf.d/20-xdebug.ini

# xdebug v2.x
xdebug.remote_connect_back=1    # Not safe for production servers

# xdebug v3.x
xdebug.log_level = 0
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_port = 9000

# 4. Install PHP Debug plugin on VSCode

On VSCode, (1) Click on Extensions tab (Ctrl+Shift+X); and (2) Install package PHP Debug by Felix Becker:

PHP Debug package

# 5. Re-start VSCode editor

# 6. Debugging

# 1. Click on Run tab (Ctrl+Shift+D)

The "Run" tab

# 2. Click on create a launch.json file

The "create launch.json" link

# 3. Click on PHP

Selecting PHP

You can close the launch.json file:  file

# 4. Click on the Play icon


The debugger tools will appear: Debugger tools

# 5. Set breakpoints (click on the ruler)

A breakpoint


Unselect Everything for performance and for your sanity.

A breakpoint


Copyright 2021 - Daniel Loureiro