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.
Linting
VS Code uses the official PHP linter [php -l
] for PHP language
diagnostics. This allows VS Code to stay current with PHP linter improvements.
Tip: Using XAMPP? Install the full version of PHP in order to obtain the development libraries.
There are three settings to control the PHP linter:
php.validate.enable
: controls whether to enable PHP linting at all. Enabled by default.php.validate.executablePath
: points to the PHP executable on disk. Set this if the PHP executable is not on the system path.php.validate.run
: controls whether the validation is triggered on save [value:"onSave"
] or on type [value:"onType"
]. Default is on save.
To change the PHP settings, open your User or Workspace Settings [⌘, [Windows, Linux Ctrl+,]] and type 'php' to filter the list of available settings.
To set the PHP executable path, select the Edit in settings.json link under PHP > Validate: Executable Path, which will open your user settings.json
file. Add the php.validate.executablePath
setting with the path to your PHP installation:
Windows
{
"php.validate.executablePath": "c:/php/php.exe"
}
Linux and macOS
{
"php.validate.executablePath": "/usr/bin/php"
}
Snippets
Visual Studio Code includes a set of common snippets for PHP. To access these, hit ⌃Space [Windows, Linux Ctrl+Space] to get a context-specific list.
PHP extensions
There are many PHP language extensions available on the VS Code Marketplace and more are being created. You can search for PHP extensions from within VS Code in the Extensions view [⇧⌘X [Windows, Linux Ctrl+Shift+X]] then filter the extensions dropdown list by typing 'php'.
Disable built-in PHP support
To disable the built-in PHP smart completions in favor of suggestions from an installed PHP extension, uncheck PHP > Suggest: Basic, which sets php.suggest.basic
to false in your settings.json
file.
Debugging
PHP debugging with XDebug is supported through a PHP Debug extension. Follow the extension's instructions for configuring XDebug to work with VS Code.
Next steps
Read on to find out about:
- Extension Marketplace - Browse the extensions others have shared
- Debugging - Learn more about VS Code debugging
9/1/2022
PHP is a web scripting language that helps us create dynamic and interactive web pages. This guide shows you how to run a PHP file in Visual Studio Code.
This tutorial is geared towards beginners and will take you through the entire process of installing PHP, setting up environmental variables, and setting up VSCode to begin running your PHP program.
Step 1: Install PHP
- Go to
windows.php.net/download to download PHP. Make sure to download the Zip file from the website. Visit the source website to download PHP.
- Right-click
on the downloaded .zip file and select Extract files…
Note: You can download and install WinRar from win-rar.comRight-click on the downloaded file and choose the Extract Files option. - When prompted to choose the Extraction Path, select C: Drive. Then, click on the New Folder button. Create a new folder in C: Drive and then Extract the file there.
- Give any name
to the folder. I will name it “php8”. Then, click OK. Enter a filename.
Step 2: Setting Up Environment Variables
To configure PHP, you must add the PHP directory to your system path. Follow these steps:
- Copy
the address of the folder we extracted 2. In my case, it’s C:\php8. Copy the address of the unzipped file.
- From the start menu, search for “Path.” When prompted with options, choose
Edit the system environment variables. Look for “path” in the Start menu, and open the first option that appears.
- Click on the Environment Variables button. Select the Environment Variables option.
- Select the Path under the “Variable” column. Then, select Edit. Select the Path option and then click on the Edit button.
- Paste the copied path and click on OK. Paste the copied path here and then click on the Ok button.
Step 3: Installing Code Runner [VSCode Extension]
Code Runner enables you to run a variety of languages in Visual Studio Code. Follow the below steps to install the Code Runner extension in VS Code:
- Launch VSCode
- From the sidebar, click on the Extensions tab. Open the Extensions tab from the sidebar.
- Type “Code Runner” in the search bar and select the first option. Search for Code Runner.
- Click on the Install button. Click on the Install button.
Step 4: Creating A PHP File
You can skip this step if you already have an existing PHP program that you want to run.
If you are creating a PHP program for the first time, follow these steps. I will be making a basic PHP program that prints “Hello World”. Follow these steps:
- Create a source
file by clicking on the New File icon. Then, type the source file name with a .php extension. For example, I will name it demo.php Inside the Explorers tab, click on the New File button.
- Write your code inside the editor. For this guide, I will use this code to print Hello World: Enter the following code in the editor.
- Save the file by pressing Ctrl + S.
Step 5: Running A PHP File
Once you have completed your PHP file, you’re ready to run the code! Follow the steps below:
- Click on the Run button on the top-right corner of the VS Code. Alternatively, you can press Ctrl + Alt + N [Command + Option + N for Mac users]. Click on the Run button.
- You’ll notice the output in the terminal. This is the output.