84

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

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
omer2802
  • 943
  • 1
  • 6
  • 6
  • @WaiHaLee https://code.visualstudio.com – mkvlrn May 03 '15 at 20:49
  • 1
    I agree with you that the documentation is very poor, hard to understand and most answer are incomplete so we have to read dozen of answers to figure out what we are doing wrong. ASP.NET debugging was 100 times easier to setup (for C# though). So was Web Matrix debugging (for PHP). – Phil1970 Dec 24 '17 at 03:09
  • I think that this tutorial might be useful: http://www.forevolve.com/en/articles/2016/08/04/php-with-visual-studio-code-and-xdebug/. I have not tried it yet. I don't understand why it is so complicate to set up **VS Code** for debugging as it was much simpler in **Visual Studio** and in **Web Matrix**. – Phil1970 Dec 24 '17 at 03:51
  • Hey You can follow the guide: https://www.cloudways.com/blog/php-debug-with-xdebug/ – Shahroze Nawaz Apr 04 '19 at 12:52
  • That guide definitely works as I've just succeeded using it https://jonathansblog.co.uk/remote-debugging-php-in-visual-studio-code-with-xdebug – 8ctopus Apr 08 '20 at 05:16

10 Answers10

59

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

flexponsive
  • 6,060
  • 8
  • 26
  • 41
  • 2
    Can I debug the PHP code running on a server, using my local VSC installation, with this? – Sushruth Jan 27 '16 at 17:07
  • 1
    It should work, but I didn't test it. Check out section "Remote Host Debugging" of the Readme – flexponsive Jan 28 '16 at 10:18
  • 7
    Installation may be straightforward. Configuration and actually getting it working is anything but that. – John Kaster Mar 13 '16 at 23:43
  • to install the plugin I needed `ext install php debug` (without the '-') but these days the command discovery is pretty awesome so finding the right plugin is easy – Robert Foster Apr 04 '16 at 11:16
  • FYI, when running a command-line script you can [enable debugging on-the-fly](http://stackoverflow.com/a/2288791/264628): `php -d xdebug.remote_autostart=1 -d xdebug.remote_enable=1 script.php`. – BrianS May 17 '16 at 03:54
  • 15
    I am the author of this extension, hit me up if you have any questions :) – felixfbecker Jun 22 '16 at 11:30
  • 1
    While this answer is true, please notice that the one by John Kaster includes this one, as the link he provides cover all setup steps. – Ignacio Segura Sep 21 '16 at 09:21
  • @felixfbecker I've started using Felix's PHP Debug VSCode extension, and it works great via Chrome (using MAMP for local development, so XDebug is enabled through that) -- but I cannot figure out how to use VSCode's Debug when executing a php script via command line. I tried running it as follows: `php script.php` `php -d xdebug.remote_autostart=1 -d xdebug.remote_enable=1 script.php` `/Applications/MAMP/bin/php/php5.6.28/bin/php script.php` `/Applications/MAMP/bin/php/php5.6.28/bin/php -d xdebug.remote_autostart=1 -d xdebug.remote_enable=1 script.php` How can I debug those? – lioman Jan 23 '17 at 15:48
  • @liomanThere is a "Launch in CLI" config in the default config and also you can launch it form the CLI and it should just work if your php.ini if configured correctly – felixfbecker Jan 23 '17 at 18:48
  • @felixfbecker There are any known issues when debugging a Laravel project? I'm getting a `DecryptException` but it is thrown only when trying to debug. – Jéf Bueno Jul 11 '17 at 18:03
  • @felixfbecker Why your extension does not start the debugger or not provide any useful information on what to do. Pressing **F5** should just works and start the appropriate server or tell me what to do if something is not properly set up. Presently, it makes us search the web for instructions during hours as it is very hard to figure out what to do as nobody explained all required steps. The only time I have done PHP in the past was with WebMatrix and everything was set up automatically. – Phil1970 Dec 24 '17 at 02:57
  • @user3096626 As many other answer, you only tell some part of the process. It is still hard to figure out what we have to do once we have installed PHP and XDebug so that pressing **F5** actual start the server and debugging and open the page in a browser so we can see the result. – Phil1970 Dec 24 '17 at 03:16
26

As far as I read about it today, you can't debug anything else than node.js, JavaScript and TypeScript at the moment, but they said they want to add new languages which you can debug. The editor is still in development. Nevertheless, I don't think there will be a php debugger in the future since php is serverside, so you can't debug it on your client alone.

If you want to debug php, I can recommend xDebug.


Updated:

Now, it is possible to debug with VS code. You need to install XDebug and php-debug extension for VScode.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46
  • 15
    This answer is quite weird but I'll try to clarify. xDebug is one of the options to debug PHP, it runs on your server as a PHP extension. Which makes one side of the story. The other side is the xDebug client. There are clients for some editors like for Sublime Text for instance. These allow you to set breakpoints on your code that will be hit when PHP is processing the request. So here what we would need would be a xDebug client for VSCode. Hope this makes it more clear. – pmmaga May 21 '15 at 15:27
  • 3
    And once one has done that, what he have to do so that pressing **F5** actually start a server and open the page for debugging. This is the hardest thing to understand but most answer don't even tell us anything about that. I have installed PHP (via IIS Web Platform Installer), I have modified my PHP.ini file, I have copied the XDebug DLL but at that point, It was still not working. – Phil1970 Dec 24 '17 at 03:13
  • @Phil1970, I assume that your explanation is awesome )) but... How come it is still not working?! I set all this up with the instructions with your perfect explanation and this didn't work also. AND I remembered that I need to start PHP file because the PHP is executable is not running without activation a script. VS Code doesn't do that. It is up to you. I assume that in your case you should "hook" a debugger to the desired PHP script and start excecution of that script. – Alexander Leon Bulatov Mar 04 '23 at 08:46
15

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.

John Kaster
  • 2,509
  • 1
  • 33
  • 40
  • I don't understand how `PHP linting` is related to debugging... And when step 4 is done, what we have to do to start debugging the code has pressing `F5` does not start anything... – Phil1970 Dec 24 '17 at 02:40
  • @Phil1970 PHP Linting could well have changed since I posted this. I do remember that PHP 7.x was required as a minimum for linting to work correctly, and it may be that VS Code has better plugin support for it now. – John Kaster Jan 03 '18 at 20:34
  • 1
    The blog post doesn't explain how to configure xdebug links to open in vscode, but it is explained here: https://stackoverflow.com/questions/52935738/how-to-configure-xdebug-stacktrace-to-link-to-and-open-files-in-vs-code – Gregory Cosmo Haun Jan 08 '19 at 18:45
15

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 (No extensions required)

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.

Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
  • This is the best answer for those who can't install extensions – dukevin Nov 09 '22 at 23:00
  • agreed. I also suggest to create a tasks.json inside the .vscode folder and use ${workspaceFolder} instead of ${fileBasename} for the php start server task – pref Feb 12 '23 at 20:44
3

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

insCode
  • 1,227
  • 14
  • 10
  • Could you provide more information on how to start the server. I was assuming that pressing **F5** to debug an application would automatically start the server (or show an appropriate message if something was wrong). By the way, I don't know if the video has the required information but **it is painful to listen**. – Phil1970 Dec 24 '17 at 02:48
3

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

emil f.
  • 93
  • 1
  • 8
  • 1
    Ha ha I just read this after spending hours trying to get it to work and noticed it just started to work when I opened a folder instead!! – Andrew Schultz Mar 04 '18 at 06:23
1

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

shibli049
  • 528
  • 12
  • 31
  • As many other answer, you don't give much information on what to do after that so that the server and a web browser would start on the specified page. – Phil1970 Dec 24 '17 at 03:17
1

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.

Archman
  • 11
  • 4
  • This question was answered 2 years ago. – Maciej Jureczko Aug 29 '17 at 16:36
  • I was thinking it was possible to use IIS to run PHP... Do I have to type the above command each time I want to debug? To me, it seems stupid that **F5** would not start the server. – Phil1970 Dec 24 '17 at 02:51
  • @MaciejJureczko Even though you might think the question was answered 2 years ago, still today the documentation is poor and information is very hard to find on the web for someone that has only used **ASP.NET + Visual Studio** and **PHP + Web Matrix** in the past. **Most other answer don't talk about starting web server.** – Phil1970 Dec 24 '17 at 03:05
  • @Archman Do we have to start the server every time and if so, should we start it before we press **F5** to start debugging. To me, it seems weird that starting debugging won't start the server. – Phil1970 Dec 24 '17 at 03:20
  • @Phil1970 We do not need to restart the server every time, F5 means start debugging, server is running all the time( F5 will not start any server). – Archman Dec 27 '17 at 06:25
  • Even if I start the server, it won't works. It give **Access Denied** errors event though I told IIS to use my account to access PHP files which reside in my Documents. – Phil1970 Dec 27 '17 at 14:55
  • I figure out that I can add **IIS_IUSRS** and then I can open the site from IIS but then I have more errors when I try to debug from VS Code. I tried `php -S` command above but when I do that, my browser is loading forever. – Phil1970 Dec 27 '17 at 15:17
1

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

encePence
  • 61
  • 2
0

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.

Rain
  • 3,416
  • 3
  • 24
  • 40