How to Use PHP With Visual Studio Code

Olorunfemi Akinlua Feb 02, 2024
  1. Use PHP With Visual Studio Code
  2. Setting Up Visual Studio Code for PHP
  3. PHP Extensions
How to Use PHP With Visual Studio Code

The most popular IDE for lots of developers is Visual Studio Code. It is a free, open-source editor for multiple languages managed and maintained by many developers and Microsoft.

Visual Studio Code is available for developers across popular operating systems from Windows and macOS to Linux. For PHP, it is a great IDE that allows us to use extensions, source control, and support tools that make developing in PHP a ton easier.

This article will discuss how to use PHP with Visual Studio Code.

Use PHP With Visual Studio Code

Install PHP

On your local PC, you can check our guide on how to set up your PHP interpreter. We discuss how to install PHP and configure PHP on your PC.

Install Visual Studio Code

Visual Studio Code (VS Code) can be downloaded on the IDE’s landing page. The site automatically detects the OS and provides the appropriate application for you.

For Linux users, you can use the command below to install VS Code on your PC. You can download the executable from the VS Code’s site for Ubuntu/Debian (.deb) and Red Hat/Fedora/SUSE.

sudo snap install code --classic

For Windows and macOS, you can go to the Windows specific and MacOS specific download pages. Once you have downloaded the installer, you can run the installer, and you are good to go.

When you open VS Code, You should see this screen regardless of your OS.

Visual Studio Code Front Screen

Setting Up Visual Studio Code for PHP

Now that you have VS Code on your system, you can configure your PHP interpreter with VS Code. Before that, let’s open a folder, php, where we can run our PHP code.

Selecting a Folder in VS Code

You may see a pop-up like the image below. Select Yes, I trust the authors

Trust Authors Pop-Up Screen

After adding your folder, you should see the following image below.

The php folder added to VS Code

Now, let’s configure the PHP interpreter with VS Code to run PHP successfully. Go to the Settings page via the image below.

Going to the Settings page

You should see a page with sections like Text Editor, Workbench, Window, etc.

Settings Page on VS Code

In the search input area, search for php.validate.executablePath, and you should see the image below. Select the link Edit in settings.json.

PHP executable path settings page in VS Code

Upon clicking the settings.json link, you should see this.

settings.json

Depending on your VS Code configurations, the line on which the php.validate.executablePath setting will differ, but in our image, it is on line 19, and as you can see, it is empty. So, we need the PHP interpreter’s path.

Depending on how you installed your PHP interpreter, the interpreter’s path may differ, but if you followed the install PHP tutorial on our site, your PHP interpreter path should be C:\php\php.exe, but because of string formatting, you can place the path just like that. You need to escape the backward slash, so the path you will add should be C:\\php\\php.exe.

Add the PHP interpreter path to VS Code

Create a new PHP file named index.php, place the code below, and run it.

<?php

echo("Hello World");

?>

Output:

Running PHP code on VS Code

Running PHP code on VS Code.

Now, you can run PHP natively within your VS Code IDE. Now, let’s beef up VS Code for you to get the most out of it.

PHP Extensions

Extensions, there are tools that lots of developers make and use to make development easy and faster within IDEs like VS Code. Three important extensions are great starting points for your PHP development. PHP Debug, PHP Intelephense, and PHP server.

To install extensions, you need to go to the Extension Tab, indicated by the red box.

Extension Tab

The first two extensions, as shown in the image below, are popular extensions that help debugging, code completion, and referencing across your codebase. In addition, Intelephense provides type analysis, suggestions, detailed documentation hover, document/range formatting, auto PHPDoc creation, and smart select, among many other features.

PHP Debug and Intelephense Extensions

The third extension, a PHP server, as the name implies, helps us server our PHP project to the browser.

PHP server extension

All you need to do is click the install button, and we have greatly improved our IDE with PHP support. Though VS Code has built-in support for syntax highlighting, basic debugging, and code indentation, they are large enough for large codebases and integrations.

With a PHP server, you don’t have to write CLI commands to start localhost, and you can easily do it with a few steps. First, you press Ctrl+Shift+P, write PHP server project, and press Enter.

Starting PHP Server

To stop the server, press Ctrl+Shift+P, write PHP stop server, and press Enter.

Stopping PHP Server

For more on PHP on Visual Studio, check VS Code PHP Documentation on linting, snippets, extensions, and debugging.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn