PHP in Visual Studio
PHP is one of the most popular programming languages for server-side web development. It’s used in many frameworks and CSMs like WordPress, Laravel, Symfony, and others which are behind a sizable chunk of the Internet.
Visual Studio is a powerful IDE, but its focus didn’t align with some languages, PHP being one of them. As time progressed more languages found their way to Visual Studio, like Python and eventually PHP. A Prague-based company called DEVSENSE developed an extension called PHP Tools for Visual Studio which has been available in the Visual Studio ecosystem for over a decade.
Since its inception PHP Tools for Visual Studio was a paid extension, offering a free trial. Now, that’s changed. DEVSENSE is releasing a big part of their offering as a free extension, leaving some features as paid ones. That allows PHP developers to work in Visual Studio and take advantage of all the great stuff which is available.
That means developers will get IntelliSense, formatting, project system, on-the-fly code analysis, composer (PHP dependency management) integration and other advanced editor-related features for free. In the latest version, PHP Tools support PHP 8.2 which hasn’t been released officially yet, and other things like generics which are a must-have for frameworks like Laravel. Other features like debugging, refactoring, unit testing, and code lens are available in the premium offering. The detailed comparison matrix can be found at https://www.devsense.com/purchase
First, install PHP Tools for Visual Studio
Create your PHP project
After the installation:
- Select Create a new project from the start page or File > New > Project… from the title bar
- In the language combo box select PHP, select PHP Web Project, then select Next
- Name your project and select Create.
- At this point, you can select the project template. Let’s choose Empty Web Site and select Next:
- Now, you can select which PHP version to set for the project. For this short tutorial, we can go with PHP 8.1 and select Finish. PHP Tools will check your environment and install and configure PHP which is set up in the project:
Use the PHP Editor in Visual Studio
When the installation is finished, the project will open. In the editor, you can hover over any symbol to get a tooltip. Or press Ctrl + Space to see the IntelliSense for the current context.
Run and Debug PHP in Visual Studio
Place a breakpoint F9 and hit F5 to run the project and start debugging. The server which is configured in the project (by default it’s PHP’s built-in Web server) will run and a browser opens. Then the breakpoint will get hit.
Congratulations! You are debugging PHP in Visual Studio.
This was a simple walkthrough of how PHP Tools for Visual Studio can be used where you have one project in the solution. A more complex setup is possible: developers can add PHP projects to their existing solutions (*.sln) – having one IDE should make their life easier, or they can work with PHP just by opening a folder. More tutorials can be found on DEVSENSE’s documentation.
If PHP development in Visual Studio is interesting to you, please install PHP Tools for Visual Studio and make sure to let the authors know how you like it, what features you are missing or if you run into anything that could be further improved. You can reach them at DEVSENSE’s PHP Tools Community Forum.