Phpstorm Vscode



Configure Xdebug in VSCode. If you want a smooth workflow with Xdebug, you can integrate it into your favorite IDE like phpstorm, Zend studio, and VScode. Let’s configure Xdebug in Visual Studio Code to debug PHP. PHP in Visual Studio Code. Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions. Web applications typically consist of both PHP and JavaScript code: PHP code runs on the server side, while JavaScript runs in the browser. With PhpStorm, you can easily debug the PHP code to inspect what is happening on the server, modify variables and so on. PhpStorm doesn't format or color code the files well but VS Code does. I use it when I need to work on that project. I also keep it up open most days as a scratch pad. Overkill, but it's nice to plop a SQL query or JSON object in and it be recognized.

Web applications typically consist of both PHP and JavaScript code: PHP code runs on the server side, while JavaScript runs in the browser. With PhpStorm, you can easily debug the PHP code to inspect what is happening on the server, modify variables and so on. We can also debug the JavaScript running in the browser by starting a JavaScript debugging session from the IDE.

This tutorial provides an overview of how you can debug PHP and JavaScript code simultaneously from within PhpStorm.

Before you start

Before you start debugging, make sure that you have a debugging engine installed and configured properly. PhpStorm supports debugging with two most popular tools: Xdebug and Zend Debugger. These tools cannot be used simultaneously because they block each other. To avoid this problem, you need to update the corresponding sections in the php.ini file as described in Configure Xdebug and Configure Zend Debugger.

Open the active php.ini file in the editor:

  1. In the Settings/Preferences dialog Ctrl+Alt+S, click PHP.

  2. On the PHP page that opens, click next to the CLI Interpreter field.

  3. In the CLI Interpreters dialog that opens, the Configuration file read-only field shows the path to the active php.ini file. Click Open in Editor.

Next, install PhpStorm debugging bookmarklets or one of the Browser debugging extensions, and the JetBrains Chrome extension extension as described in Live Edit in HTML, CSS, and JavaScript.

Listening for incoming debugger connections

In PhpStorm, enable listening to incoming debug connections by either clicking on the toolbar or selecting Run | Start Listening for PHP Debug Connections in the main menu. This will ensure PhpStorm reacts when a debugging session is started and opens the Debug tool window automatically. Before launching the script, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings/Preferences dialog Ctrl+Alt+S.

Start the JavaScript debugger

Depending on your preference or application requirements, you can use the PhpStorm's built-in webserver to run our application locally, or make use of any other web server running either locally or on a remote machine.

Use the built-in web server

The JavaScript debugger in PhpStorm can be started from the editor or from the Project tool window by means of the Debug | <filename> context menu command. If the selected file is a PHP file, two entries will be available. It is important to select the first one marked with , which will start the JavaScript debugger.

Phpstorm Vscode Theme

Once started, we can place breakpoints in JavaScript code and use the JavaScript debugger.

Use an external web server

When using a local web server, such as Apache or Nginx, or when developing on a remote web server, a Vagrant or a Docker machine, we can start the JavaScript debugger using a run/debug configuration.

Create a Run/Debug configuration

  1. Do any of the following:

    • Select Edit Configurations on the PhpStorm toolbar

    • Select Run | Edit Configurations from the main menu.

  2. In the Run/debug configurations dialog dialog that opens, click on the toolbar and add a new JavaScript Debug configuration.

  3. Enter the full URL to the page we want to debug on the web server. Optionally, provide some mappings so PhpStorm can determine where to find local files relative to the remote URL. This is only required when we have a different project structure locally and on the remote server. Note that if you are deploying PHP applications with PhpStorm, mappings will be reused from the deployment configuration.

Once the configuration is created, you can start the JavaScript debugging session from the PhpStorm toolbar:

Start a PHP debugging session from the browser

We'll follow the Zero-configuration debugging approach. In the browser, we can use PhpStorm debugging bookmarklets or one of the Browser debugging extensions to start a PHP debugging session. This will instruct the PHP server to make a connection to PhpStorm and open the debugger. Note that the IDE may initially ask you to provide the necessary path mappings. Once the debugger is attached, we will be able to debug both JavaScript and PHP at the same time. PhpStorm will switch between the debuggers as necessary.

Launch the JavaScript and PHP debugger at the same time

In the previous steps, we started the JavaScript and PHP debugger separately. When using Xdebug, we can pass a XDEBUG_SESSION_START URL parameter to our server to start PHP debugging simultaneously with JavaScript debugging. We can do this using a customized run/debug configuration. Create the run/debug configuration as you've done earlier, and make sure to append the XDEBUG_SESSION_START=some-session-name URL parameter, for example, ?XDEBUG_SESSION_START=phpstorm:

Troubleshooting

I cannot place breakpoints in the JavaScript parts of a php file

Currently, setting both PHP and JavaScript breakpoints in one file is not supported. For example, no JavaScript breakpoints can be set in the following code:

<?php // ... ?><!doctype html> <html> <head> <script> /* javascript code */ </script> </head> <body> </body> </html>

To be able to debug the PHP and JavaScript code simultaneously, move the JavaScript code into a separate .js file and reference it from HTML:

<?php // ... ?><!doctype html> <html> <head> <script src='index.js'></script> </head> <body> </body> </html>

We can then place PHP breakpoints in the php file, and set JavaScript breakpoints in the js file.

PhpStorm integrates with compilers that translate Sass, Less, and SCSS code into CSS. To use a compiler in PhpStorm, you need to configure it as a File Watcher based on the relevant predefined template.

You can also use the compiler from the command line or configure it as a third-party tool, see Configuring Third-Party Tools for details.

Before you start

  1. Make sure you have Node.js on your computer.

  2. Make sure the File Watchers and Less or Sass bundled plugins are enabled on the Settings/Preferences | Plugins page, see Managing plugins for details.

Installing Sass/SCSS

  • In the embedded Terminal (Alt+F12), type:

    npm install -g sass

    Learn more from the Sass official website.

You can also install the sass package on the Node.js and NPM page as described in npm, pnpm, and Yarn.

Installing Less

  • In the embedded Terminal (Alt+F12), type:

    npm install --global less

    Learn more from the Less official website.

Webstorm Vs Vscode

You can also install the less package on the Node.js and NPM page as described in npm, pnpm, and Yarn.

Compiling your code into CSS

Phpstorm Vscode

Phpstorm Vs Vscode

To compile your code automatically, you need to configure a Sass, Less, or SCSS File Watcher which will track changes to your files and run the compiler.

When you open a file, PhpStorm checks whether an applicable File Watcher is available in the current project. If such File Watcher is configured but disabled, PhpStorm displays a popup that informs you about the configured File Watcher and suggests to enable it.

If an applicable File Watcher is configured and enabled in the current project, PhpStorm starts the compiler automatically upon the event specified in the New Watcher dialog.

  • If the Auto-save edited files to trigger the watcher checkbox is selected, the File Watcher is invoked as soon as any changes are made to the source code.

  • If the Auto-save edited files to trigger the watcher checkbox is cleared, the File Watcher is started upon save (File | Save All, Ctrl+S) or when you move focus from PhpStorm (upon frame deactivation).

Phpstorm Vs Vscode For Laravel

Learn more from File watchers.

PhpStorm creates a separate file with the generated output. The file has the name of the source Sass, Less, or SCSS file and the extension .css. The location of the generated files is defined in the Output paths to refresh field of the New Watcher dialog. However, in the Project Tree, they are shown under the source file which is now displayed as a node.

Create a File Watcher

  1. In the Settings/Preferences dialog Ctrl+Alt+S, click File Watchers under Tools. The File Watchers page that opens shows the list of already configured File Watchers.

  2. Click or press Alt+Insert. Depending on the tool you are going to use, choose the Less, Sass, or SCSS predefined template from the list.

  3. In the Program field, specify the path to the compiler archive depending on the chosen predefined template.

    • lessc for Less.

    • sass for Sass/SCSS.

    If you followed the standard installation procedure with npm, PhpStorm locates the required files itself and fills in the field automatically. Otherwise, type the path manually or click and choose the file location in the dialog that opens.

  4. Proceed as described in File watchers.

Configuring syntax highlighting

Phpstorm Vs Codeine

You can configure Less/Sass/SCSS-aware syntax highlighting according to your preferences and habits.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | Color Scheme | Less/Sass/SCSS.

  2. Select the color scheme, accept the highlighting settings inherited from defaults or customize them as described in Configuring colors and fonts.