Debugging PHP/Laravel with Visual Studio Code
By: Ali Raza Lilani
Tags: Vscode Extension, PHP, Xdebug, Vscode, Debugging

Installing Xdebug PHP extension

The first step is to download and install the Xdebug PHP plugin. To do so, create a file with phpinfo() like this

<?php phpinfo(); ?>

you will get output like this .

Copy this all text ( Ctrl + A ) then ( Ctrl + C ) then go to this https://xdebug.org/wizard.php . This wizard lets you paste the contents of a phpinfo() call, and it will tell you exactly what files to download, where to put them, and what value you have to set for zend_extension in your php.ini file to successfully install the extension.

Updating php.ini file

Then you’ll need to change the php.ini file in your php folder. If you followed the instructions above, you should have your php.ini path in instruction. If you’re using Xampp on Windows, it’ll usually be found here: C: xampp\php\php.ini

add following lines of code in your php.ini

[xDeubg]

xdebug.mode=debug

xdebug.start_with_request=Yes

xdebug.client_port=9001

xdebug.remote_port=9001

xdebug.log=”{path where you want to save logs}”

xdebug.log_level=7

xdebug.idekey=VSCODE

xdebug.remote_enable=1

xdebug.remote_autostart=1

xdebug.client_host=”127.0.0.1"

zend_extension=”C:\xampp\php\ext\php_xdebug.dll”

zend_extension = “{path where you saved download file in php\ext\file_name.dll}” or xdebug wizard has given you the path to put in zend_extension if you followed the instructions above.

save and close the php.ini file and restart your apache server.

Installing PHP debugging extension

The PHP debugging extension must then be installed. To do so, choose “View” from the toolbar and then “Command Palette,” or press ( Ctrl + Shift + P ) on Windows. Type “ext install” into the drop-down box and press Enter.

ext install

Then hit enter after typing “PHP Debug” and selecting the extension suitably called “PHP Debug.”

PHP DEBUG

Visual Studio Code will restart when the debugging plugin is installed. If not, reload it manually.

Visual Studio Code debugging setup

Now we need to enable debugging in Visual Studio Code. Open the folder containing your PHP code in Visual Studio. To configure our debugging environment, select the Debug View from the left hand side and then click the Run and Debug button. Select PHP from the environment drop-down menu that appears, as seen in the image below.

click on RUN and Debug on left and select environment drop down will appear on right — select PHP

When the Debug button is pressed for the first time, a debugging configuration file launch.json is created in .vscode directory.

launch.json file in .vscode directory

You may leave this file as is and not worry about it again if you simply want to debug the current opened script, but if you want to activate listen for xdebug (which implies you want to debug when the page loads or the API is used), you must complete this additional step.

you need to add this key value (for mapping your directory) in second object of configurations array

“pathMappings”: { “C:/xampp/htdocs/{your project-name}” : “${workspaceFolder}” },

like this

This step is critical for “Listen for Xdebug” debugging to work.

Select “Listen for XDebug” from the left debugger menu, then click the Debug button again. If everything is set up correctly, Visual Studio Code will now be in debugging mode!

Finally DEBUGGING !!! 😀

We now need to add some breakpoints to our code in order for the debugger to be effective. Simply click in the gutter adjacent to the line you want to breakpoint on to establish a breakpoint.

Breakpoints in Visual Studio Code are represented by a red dot, as they are in most debuggers. If we now load the page in the browser, the page’s execution will come to a halt at our breakpoint, allowing us to view the page’s present state.