How to Run PHP in Visual Studio Code

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

  1. Go to windows.php.net/download, and download the Zip file from the website.

    Download PHP
    Get the .Zip PHP download package.

  2. 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.
  3. Choose the Extraction Path. For this example, we’ll select Windows (C:) drive. Then, click on the New folder button.

    Extraction Path
    Create a new folder in your C drive.

  4. Give any name to the folder. For example, I will name it “php8”. Then, click OK.

    Filename
    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:

  1. Copy the path of the PHP folder we made previously. In my case, it’s C:php8

    Copy Address
    Copy the address of the PHP folder.

  2. From the start menu, search for “Path”. Then, from the options, choose Edit the system environment variables.

    Search Path
    Search for “path” in the Start menu, and open Edit the system environment variables.

  3. Click on the Environment Variables button.

    Environment Variables
    Select the Environment Variables button.

  4. Under the “Variable” column, select Path. Then, click Edit.

    Edit
    Select the Path option, then click Edit.

  5. Paste the copied path, then click OK.

    Paste Address
    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:

  1. Launch VSCode.
  2. From the sidebar, click on the Extensions tab.

    Extensions Tab
    Open the Extensions tab from the sidebar.

  3. Type “Code Runner” in the search bar and select the first option.

    Search Code Runner
    Search for Code Runner.

  4. Click on Install.
    Install Code Runner
    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”.

  1. 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

    New File
    Inside the Explorers tab, click the New File button and create a new PHP file.

  2. Write your code inside the editor. For this guide, I will use this code to print Hello World:
    <?php echo "Hello World"; ?>

    Code
    Enter the following code in the editor.

  3. 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:

  1. 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).

    Run
    Click on the Run button.

  2. You’ll notice the output in the terminal.

    Output
    This is the output.

Leave a Comment