[php] How to run or debug php on Visual Studio Code (VSCode)

I can't find a way to run or debug php on Visual studio code, Does anyone know how?

This question is related to php debugging visual-studio-code

The answer is


There is a much easier way to run PHP, no configuration needed:

  1. Install the Code Runner Extension
  2. Open the PHP code file in Text Editor
    • use shortcut Ctrl+Alt+N
    • or press F1 and then select/type Run Code,
    • or right click the Text Editor and then click Run Code in editor context menu
    • or click Run Code button in editor title menu
    • or click Run Code button in context menu of file explorer

Besides, you could select part of the PHP code and run the code snippet. Very convenient!


To debug php with vscode,you need these things:

  1. vscode with php debuge plugin(XDebug) installed;
  2. php with XDebug.so/XDebug.dll downloaded and configured;
  3. a web server,such as apache/nginx or just nothing(use the php built-in server)

you can gently walk through step 1 and 2,by following the vscode official guide.It is fully recommended to use XDebug installation wizard to verify your XDebug configuration.

If you want to debug without a standalone web server,the php built-in maybe a choice.Start the built-in server by php -S localhost:port -t path/to/your/project command,setting your project dir as document root.You can refer to this post for more details.


XDebug changed some configuration settings.

Old settings:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000

New settings:

xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9000

So you should paste the latter in php.ini file. More info: XDebug Changed Configuration Settings


The best solution for me was to add a key binding to run PHP code directly in the terminal

To do so you just need to download terminal-command-keys from VS code extensions marketplace:

enter image description here

Then got to File>Preferences>Keyboard Shortcuts and click on the following icon at the upper right corner:

enter image description here

It will open up the keybindings.json file

Add the following settings

[
    {
        "key": "ctrl+s",
        "command":"terminalCommandKeys.run",
        "when": "editorLangId == php",
        "args": {
            "cmd":"php ${file}",
            "newTerminal":true,
            "saveAllfiles": true,
            "showTerminal": true,
        }
    }
]

key is the shortcut to run your PHP file (I use ctrl+s) you can change it as you wish

when to run different commands for different file types (I set it for PHP files only) vscode's "when" clauses

See the full settings documentation from here

That's it, I hope it helps.


already their is enough help full answers but if you want to see the process then
[ click here ]

Steps in Short

[ good to go ]

  1. make sure that you have restarted your local server

source : https://www.youtube.com/watch?v=8MLEB1qx984


If you are using Ubuntu 16.04 and php7 you can install xdebug with below command:

sudo apt-get install php-xdebug

You can find the full configuration process here.

If you are using windows, you can download xdebug from xdebug.org.

And start debugging in VS-code with php-debug extension.

how to configure php-debug for xdebug


Debugging PHP with VSCode using the vscode-php-debug extension

VSCode can now support debugging PHP projects through the marketplace extension vscode-php-debug.

This extension uses XDebug in the background, and allows you to use breakpoints, watches, stack traces and the like:

Screenshot: PHP Debugging in VSCode using vscode-php-debug extension

Installation is straightforward from within VSCode: Summon the command line with F1 and then type ext install php-debug


It's worth noting that you must open project folder in Visual Studio Code for the debugger to work. I lost few hours to make it work while having only individual file opened in the editor.

Issue explained here


If you don't want to install xDebug or other extensions and just want to run a PHP file without debugging, you can accomplish this using build tasks.

Using Build Tasks

First open the command palette (Ctrl+Shift+P in Windows, ?+Shift+P in Mac), and select "Tasks:Open User Tasks". Now copy my configuration below into your tasks.json file. This creates user-level tasks which can be used any time and in any workspace.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Start Server",
            "type": "shell",
            "command": "php -S localhost:8080 -t ${fileDirname}",
            "isBackground": true,
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "Run In Browser",
            "type": "shell",
            "command": "open http://localhost:8080/${fileBasename}",
            "windows": {
                "command": "explorer 'http://localhost:8080/${fileBasename}'"
            },
            "group": "build",
            "problemMatcher": []
        }
        {
            "label": "Run In Terminal",
            "type": "shell",
            "command": "php ${file}",
            "group": "none",
            "problemMatcher": []
        }
    ]
}

If you want to run your php file in the terminal, open the command palette and select "Tasks: Run Task" followed by "Run In Terminal".

If you want to run your code on a webserver which serves a response to a web browser, open the command palette and select "Tasks: Run Task" followed by "Start Server" to run PHP's built-in server, then "Run In Browser" to run the currently open file from your browser.

Note that if you already have a webserver running, you can remove the Start Server task and update the localhost:8080 part to point to whatever URL you are using.

Using PHP Debug

Note: This section was in my original answer. I originally thought that it works without PHP Debug but it looks like PHP Debug actually exposes the php type in the launch configuration. There is no reason to use it over the build task method described above. I'm keeping it here in case it is useful.

Copy the following configuration into your user settings:

{
    "launch": {
        "version": "0.2.0",
        "configurations": [
            {
            "type": "php",
            "request": "launch",
            "name": "Run using PHP executable",
            "program": "${file}",
            "runtimeExecutable": "/usr/bin/php"
            },
        ]
    },
    // all your other user settings...
}

This creates a global launch configuration that you can use on any PHP file. Note the runtimeExecutable option. You will need to update this with the path to the PHP executable on your machine. After you copy the configuration above, whenever you have a PHP file open, you can press the F5 key to run the PHP code and have the output displayed in the vscode terminal.


There is now a handy guide for configuring PHP debugging in Visual Studio Code at http://blogs.msdn.com/b/nicktrog/archive/2016/02/11/configuring-visual-studio-code-for-php-development.aspx

From the link, the steps are:

  1. Download and install Visual Studio Code
  2. Configure PHP linting in user settings
  3. Download and install the PHP Debug extension from the Visual Studio Marketplace
  4. Configure the PHP Debug extension for XDebug

Note there are specific details in the linked article, including the PHP values for your VS Code user config, and so on.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to visual-studio-code

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined raised when starting react app Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error Cannot edit in read-only editor VS Code How to setup virtual environment for Python in VS Code? Pylint "unresolved import" error in Visual Studio Code Why do I keep getting Delete 'cr' [prettier/prettier]? How to set up devices for VS Code for a Flutter emulator VSCode single to double quote automatic replace js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check How can I clear the terminal in Visual Studio Code?