PHP is a popular web scripting language for creating dynamic and interactive web pages. VS Code has a great UI for coding and testing your PHP file, but it can be challenging to first set it up.
This guide takes you through the full process of installing PHP, setting up environmental variables, installing required extensions, and setting up VSCode to begin running your PHP file.
Step 1: Install PHP
- Go to windows.php.net/download, and download the Zip file from the website.
- Next, you need to extract the package. We recommend using WinRAR for this. Right-click the downloaded zip file and select Extract files…
Right-click the downloaded file and select Extract files.
- Choose the Extraction Path. For this example, we’ll select Windows (C:) drive. Then, click on the New folder button.
- Give any name to the folder. For example, I will name it “php8”. Then, click OK.
Step 2: Set up the environment variables
To configure PHP, you must add the PHP directory to your system path. Follow these steps:
- Copy the path of the PHP folder we made previously. In my case, it’s C:php8
- From the start menu, search for “Path”. Then, from the options, choose Edit the system environment variables.
- Click on the Environment Variables button.
- Under the “Variable” column, select Path. Then, click Edit.
- Paste the copied path, then click OK.
Step 3: Install Code Runner (VSCode extension)
Code Runner enables you to run various coding languages in Visual Studio Code, including PHP. Follow the below steps to install the Code Runner extension in VS Code:
- Launch VSCode.
- From the sidebar, click on the Extensions tab.
- Type “Code Runner” in the search bar and select the first option.
- Click on Install.
You now have Code Runner installed in VS Code.
Step 4: Create a new PHP file
You can skip this step if you already have an existing PHP file you want to run.
If you are creating a new PHP file, follow these steps. In this example, I will make a basic PHP program that prints “Hello World”.
- Create a new PHP file by clicking on the New File icon. Then, type the file name, ending with a .php extension. For example, I will name it demo.php
- Write your code inside the editor. For this guide, I will use this code to print Hello World:
<?php echo "Hello World"; ?>
- Save the file by pressing Ctrl + S.
Step 5: Run your 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 VS Code. Alternatively, you can press Ctrl + Alt + N (Command + Option + N for Mac users).
- You’ll notice the output in the terminal.