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.
Get the .Zip PHP download package. - 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.
Create a new folder in your C drive. - Give any name to the folder. For example, I will name it “php8”. Then, click OK.
Enter a filename, 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
Copy the address of the PHP folder. - From the start menu, search for “Path”. Then, from the options, choose Edit the system environment variables.
Search for “path” in the Start menu, and open Edit the system environment variables. - Click on the Environment Variables button.
Select the Environment Variables button. - Under the “Variable” column, select Path. Then, click Edit.
Select the Path option, then click Edit. - Paste the copied path, then click OK.
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.
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 Install.
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
Inside the Explorers tab, click the New File button and create a new PHP file. - Write your code inside the editor. For this guide, I will use this code to print Hello World:
<?php echo "Hello World"; ?>
Enter the following code in the editor. - 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).
Click on the Run button. - You’ll notice the output in the terminal.
This is the output.