How to run or debug php on Visual Studio Code (VSCode) - php
I can't find a way to run or debug php on Visual studio code, Does anyone know how?
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:
Installation is straightforward from within VSCode: Summon the command line with F1 and then type ext install php-debug
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.
There is a much easier way to run PHP, no configuration needed:
Install the Code Runner Extension
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!
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:
Download and install Visual Studio Code
Configure PHP linting in user settings
Download and install the PHP Debug extension from the Visual Studio Marketplace
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.
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.
already their is enough help full answers but if you want to see the process then
[ click here ]
Steps in Short
download php debug plugin [ https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug ]
download xDebug.dll [ https://xdebug.org/wizard.php ]
move xdebug file to [ ?? / php / ext / here ]
update php.ini file with following lines :
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
zend_extension=path/to/xdebug
[ good to go ]
make sure that you have restarted your local server
source : https://www.youtube.com/watch?v=8MLEB1qx984
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 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.
To debug php with vscode,you need these things:
vscode with php debuge plugin(XDebug) installed;
php with XDebug.so/XDebug.dll downloaded and configured;
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:
Then got to File>Preferences>Keyboard Shortcuts and click on the following icon at the upper right corner:
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.
Related
Xdebug not starting when use the XDEBUG_TRIGGER env on VS Code
I'm configuring Xdebug in VS Code and it doesn't work when I use the start_with_request=trigger setting. If I set the config value to yes it works. I think VS Code is not sending the env variables from the launch.json file. My Xdebug ini file: zend_extension=xdebug.so xdebug.mode = debug xdebug.start_with_request = trigger My launch.json: { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003, "env": { "XDEBUG_TRIGGER": "true" } } ] } I use VSCode with Microsoft Remote-SSH extension on a Ubuntu VM and Windows 10. My Xdebug extension is the XDebug official extension. Log from Xdebug: [2393] Log opened at 2022-03-29 13:54:04.173262 [2393] [Config] DEBUG: Checking if trigger 'XDEBUG_TRIGGER' is enabled for mode 'debug' [2393] [Config] INFO: Trigger value for 'XDEBUG_TRIGGER' not found, falling back to 'XDEBUG_SESSION' [2393] [Config] INFO: Trigger value for 'XDEBUG_SESSION' not found, so not activating [2393] [Config] DEBUG: Checking if trigger 'XDEBUG_TRIGGER' is enabled for mode 'debug' [2393] [Config] INFO: Trigger value for 'XDEBUG_TRIGGER' not found, falling back to 'XDEBUG_SESSION' [2393] [Config] INFO: Trigger value for 'XDEBUG_SESSION' not found, so not activating [2393] Log closed at 2022-03-29 13:54:04.360936
vscode-php-debug dev here. This is a common misunderstanding. The provided launch.json only listens for Xdebug/DBGp connections - as the default name implies. If there was also a program setting in there, VS Code would also start a (php) process. And only in that case could VS Code contribute to the environment of that newly started process. So, assuming you are running a web server, you can only change Xdebug settings by modifying the php.ini or somehow change the process environment of that web server process... You can also check out other launch.json snippers by typing php at the end of the current snippet. Maybe some of them could be useful. Perhaps I should add a warning if somebody sets env without setting program...
sublime text and breakpoints on remote PHP debugging
I have a FreeBSD server running Apache with PHP and Xdebug, and a Mac OS client running Sublime 3.2.2 for development and debugging. Code files are exchanged between server and client using SFTP. I can execute the code stepwise by setting "break_on_start": true in my Xdebug.sublime-settings. Stack trace, context, breakpoints, everything is there: debugging works in principle. However, stopping execution at a breakpoint does not work. Also, the current position in the code is not shown during debugging (the little green or yellow arrow left to the code lines). This limits debugging to stepping through the code right from the beginning and reading the current code position from the stack. My suspicion is that the PHP code must be enriched with debug information (i.e. breakpoints) before upload but I cannot find a way to do this.
Wesley put me on the right track, problem was the mapping between local and remote files. I finally got it working with these project settings: { "folders": [ { "path": "." }, ], "settings": { "xdebug": { "url": "http://<myserver>/<serverpath>/<mytestfile>.php", "path_mapping": { "<server path to php files>" : "<local path to php files>", } } } } The problem could be seen in the server's xdebug.log file, which reported breakpoints with reference to the client's file system instead of the server's: [49644] <- breakpoint_set -i 11 -n 8 -f file%3A///Users/<foo>/Documents/Development/foo/xdebugtest.php -t line After fixing the path_mapping breakpoint definitions like this can were reported: [49638] <- breakpoint_set -i 8 -n 5 -f file%3A///usr/local/www/<foo>/xdebugtest.php -t line Note that the way of synching local and remote files (here: SFTP plugin) was irrelevant to the problem.
How to set php executable path php.validate.executablePath in vscode when php is set inside docker container?
I have a development environment based in docker. Everytime that I open VSCode I get this message: Cannot validate since no PHP executable is set. Use the setting 'php.validate.executablePath' to configure the PHP executable. Somebody know how to set php.validate.executablePath in this case? I have no idea how to do it.
Here is a screenshot. Don't forget to escape \ You don't have to add it to the Path
For linux users: if you don't have PHP installed then first download, then in terminal type $ whereis php and it will show path for php executable (It will be either in /usr/bin/php or usr/local/bin/php) which you can copy from terminal. In VScode goto settings.json file and paste php.validate.executablePath: /usr/bin/php
In my case using XAMPP is like this "php.validate.executablePath": "C:/xampp/php/php.exe" hope that answer your question
Sure, easy. Just follow what it's says. Go to File> Preferences > Settings ... it will open the settings.json file. add the following code: {"php.validate.executablePath": "Here you put your PHP.exe path"} You have to know where te PHP.exe file is in your computer, search in the php>bin folder. Hope it works for you
I had the same problem but found the fix here. In Windows: Go To System Properties Go To Advanced Tab Click "Environment Variables" Select Path Add a new path that points to your php 7 executable:
My case, I'm using Laragon, so you should search where is your php.exe. By VSCode just add. "php.executablePath": "C:\\laragon\\bin\\php\\php-7.2.19-Win32-VC15-x64\\php.exe" on settings.json.
VSCode Linux Users: Step 1: In your terminal type whereis php In my case "php: /opt/lampp/bin/php" Step 2: { "php.validate.executablePath": "/opt/lampp/bin/php" }
Download PHP, in my case, I downloaded from here: http://windows.php.net/download/ Copy and paste the files to a location on your computer, and set the path: "php.validate.executablePath": "C:/php/php.exe"
"php.validate.executablePath": "C:/php/php.exe" wont work due to incorect escaping try this; "php.validate.executablePath": "C:\\php\\php.exe"
If you get this on windows in VS code click on the blue 'open settings' button. Near the bottom of the screen this opens there's a link to enter the settings, click this to open the settings.json file. Edit the file to look like this: { "php.executablePath" : "C:/php-7.4.2-nts-Win32-vc15-x64/php.exe", "php.validate.executablePath" : "C:/php-7.4.2-nts-Win32-vc15-x64/php.exe" } where C:/php-7.4.2-nts-Win32-vc15-x64 is the path to your PHP dir
I don't know if this helps but I ran into the same problem with the extension PHP Intelephense in Visual Studio Code. What I did is read the instructions so the extension could work properly. So I disabled PHP Language Features by searching "#builtin PHP" in the extensions. Also, other (3rd party) PHP extensions that provide similar functionality should also be disabled for best results. Finally, I added glob patterns for non-standard PHP file extensions to the files.associations setting. For example: "files.associations": { "*.module": "PHP" } Doing this made the error go away. I'm new to PHP too, let me know if this is the correct way of solving this problem with PHP Intelephense.
If you're using VScode and have installed Php through MAMP, the default path should be: C:/MAMP/bin/php/php7.4.1/php.exe / not \
Regarding the docker-specific revision of the question, I found this thread in the VS Code PHP Intellisense git repo issue that explains how to create a wrapper script. It will involve mapping volumes, though, so take that into account. https://github.com/felixfbecker/vscode-php-intellisense/issues/150#issuecomment-330374124
I have ubuntu and XAMPP. The following worked for me. Press CTRL + P Type settings.json and hit ENTER. Add the following line in settings.json { ... "php.validate.executablePath": "/opt/lampp/bin/php", ... } Make sure to proper syntax.
I simply used double forward slashes and it worked, like so: "php.validate.executablePath": "C:\\xampp\\php\\php.exe",
Unix / Linux / Mac / WSL , Step 1 : Find PHP path whereis php Response will show you where your php directories are. user#User:~$ whereis php php: /usr/bin/php /usr/bin/php8.0 /usr/bin/php8.1 /usr/lib/php /etc/php /usr/share/php8.0-opcache /usr/share/php8.0-xml /usr/share/php8.1-curl /usr/share/php8.1-opcache /usr/share/php8.0-gd /usr/share/php7.0-gd /usr/share/php7.0-common /usr/share/php8.0-readline /usr/share/php8.0-mysql /usr/share/php8.0-zip /usr/share/php8.1-mbstring /usr/share/php8.1-xml /usr/share/php8.0-mbstring /usr/share/php8.0-common /usr/share/php8.0-curl /usr/share/php8.1-readline /usr/share/php8.1-common /usr/share/man/man1/php.1.gz Step 2 : Configure VS-Code Go to -> "Preference -> Settings" Search for -> "php path" Then click -> "Edit in settings.json" In my case I had multiple.So I chose the one I wanted from the available versions and my VS-Code looked like this "php.validate.executablePath": "/usr/bin/php8.1",
IF using Laragon...Dont forget to change \ to / "php.executablePath": "C:/laragon/bin/php/php-7.4.5-Win32-vc15-x64/php.exe",
Go to 'Preference' -> 'Settings' Search for> php path Then click Edit in settings.json Then just put your directory with (DOUBLE Backslash). Here is an example of mine: C:\\php\\php.exe Here is the format: drive:\\folder_name\\php.exe This should fix the problem.
php.validate.executablePath in laragon In my case using laragon is like this "php.validate.executablePath": "C:\\laragon\\bin\\php\\php-7.4.19-Win32-vc15-x64\\php.exe" php-7.4.19-Win32-vc15-x64 : Note that this part of your file name may be different
I also had this error. I have installed Laragon and this addition in setting.json worked for me. "php.executablePath":"C:/laragon/bin/php/php-7.2.19-Win32-VC15-x64/php.exe"
"php.validate.executablePath": "C:\xamp\php\php.exe" this is working for this case
For Wampserver: Inside settings.json "php.validate.executablePath": "C:/wamp64/bin/php/php7.4.26/php.exe"
If php is inside a docker container and you don't need it or don't want to install it on you local system, you can create a php wrapper script like it is suggested here: https://github.com/felixfbecker/vscode-php-intellisense/issues/471 I prefer to use the remote container Microsoft extension on vscode and work inside the container itself.
I've used such solution: First, create a php file inside /usr/local/bin/ and make it executable: sudo touch /usr/local/bin/php sudo chmod +x /usr/local/bin/php Update content of it with: docker exec $(docker ps --filter name=-app --format '{{.ID}}') php "$#" So this will find a container with the name -app at the end. I'm using it along with the docker compose where I have php inside some container which has -app at the end of the name. You can check more about filter here. And then update php.validate.executablePath with /usr/local/bin/php: { "php.validate.executablePath": "/usr/local/bin/php" }
I also got this error message. The path to php.exe was already set in my System Environment Variables. The error went away when I commented out "php.validate.executablePath": "c:/path/to/php7.2.10/php.exe", in the settings.json file in VSCode.
Drupal 8: Debugging tests with XDebug and PHPStorm - cennot step through
I'm trying to debug my Drupal 8 tests with XDebug, but if I run them with XDebug switched on, I cannot step into the test. I execute the following command: vendor/bin/phpunit -c core modules/permissions_by_term/tests/src/Kernel/SelectTermTest.php PHPUnit reports me here: "Can't find a source position. Server name 'localhost' doesn't exist." My settings in the PHP.ini file are looking as follows: xdebug.remote_enable=true xdebug.profiler_enable=0 xdebug.idekey=PHPSTORM xdebug.max_nesting_level=256 xdebug.remote_autostart=true Can anybody share here some experience?
I got it to run. I had to do some more settings. First, create a new PHP Remote Debug under the Run Configurations (The ZeroConfiguration didn't work for me). Define a Server with the name you want (here "TestServer") and enter PHPSTORM as Ide Key. Also see the Jetbrains documentation: https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+Web+Applications+with+Run+Debug+Configurations Enable the option "Break at first line in PHP scripts" in under the RUN menu . It can be possible that you need to override the file mapping. (In my case PHPStorm is not able to detect file mapping automatically) (See: https://www.jetbrains.com/help/phpstorm/10.0/override-server-path-mappings-dialog.html). It's is within same dialog where the server was created. Start the Remote Debug Session with clicking on the corresponding icon. Then you should be able to run PHPUnit with this command: PHP_IDE_CONFIG="serverName=TestServer" XDEBUG_CONFIG="idekey=PHPSTORM" vendor/phpunit/phpunit/phpunit -c YOUPHPUNITXMLCONFIG Set the PHP_IDE_CONFIG corresponding to your settings. Set breakpoints where you want to stop. I hope I could help you. Nico
xdebug not running on mamp
I am trying to run xdebug on mamp and followed many tutorials but when i see it in my phpinfo() i don't find xdebug. As xdebug is already included in the mamp as i read.This is line i added in my php.ini and i also went to that location to see if xdebug.so exist or not.but still its not working. any help? i am using mamp 2.1.2 p.s I modified the php version to mine in php.ini from php5 to php5.4.10
I don't know if it's too late but i'm sure that someone will need the real answer. To solve yo online have to change php.ini in the correct path... For php 5.4.10 there are two php.ini and I changed both and it works for me. They are in: MAMP/conf/php5.4.10/php.ini and MAMP/bin/php/php5.4.10/conf/php.ini When I changed the second one, I reload MAMP and IT WORKS!! If you want to know if it's working, open MAMP on localhost, click phpinfo and find xdebug. Also, I have to say that I changed in httpd.conf (MAMP/conf/apache) the port 8888 to 80 (you have to change Listen:8888 to Listen:80 and local_host:8888 to local_host:80. (without _) In that case, yo only have to go to http: / / localhost to see your projects
Thanks #titolancreo!! I was about to give up for today after a couple hours and just by adding the same line in both php.ini it suddenly worked! you can also do this in your terminal to know if it works: php -m You should be able to see the Xdebug module two times, one in the [PHP Modules] list and another one in the [Zend Modules] list.
You also have to check - (you already did I see) - whether the xdebug.so file exists in the location specified in php.ini. In my case the original line was: zend_extension="/Applications/MAMP/bin/php/php7.1.0/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so" I finally changed it to point to the correct location and it worked: zend_extension="/Applications/MAMP/bin/php/php7.1.0/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so" No error messages however in php_error.log. I can't figure out why certain .so files are reported as missing in the log and some others aren't, like this one.
make sure that you are listening to the correct port from php.info for me was xdebug.remote_port=9900 then in vcode { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9900 },