How To Run A PHP File in Visual Studio Code

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

  1. Go to windows.php.net/download to download PHP. Make sure to download the Zip file from the website.

    Download PHP
    Visit the source website to download PHP.
  2. Right-click on the downloaded .zip file and select Extract files…
    Note:
    You can download and install WinRar from win-rar.com

    Extract Files
    Right-click on the downloaded file and choose the Extract Files option.

     

  3. When prompted to choose the Extraction Path, select C: Drive. Then, click on the New Folder button.

    Extraction Path
    Create a new folder in C: Drive and then Extract the file there.
  4. Give any name to the folder. I will name it “php8”. Then, click OK.

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

  1. Copy the address of the folder we extracted 2. In my case, it’s C:\php8.

    Copy Address
    Copy the address of the unzipped file.
  2. From the start menu, search for “Path.” When prompted with options, choose Edit the system environment variables.

    Search Path
    Look for “path” in the Start menu, and open the first option that appears.
  3. Click on the Environment Variables button.

    Environment Variables
    Select the Environment Variables option.
  4. Select the Path under the “Variable” column. Then, select Edit.

    Edit
    Select the Path option and then click on the Edit button.
  5. Paste the copied path and click on OK.

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

  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 the Install button.

    Install Code Runner
    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:

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

    New File
    Inside the Explorers tab, click on the New File button.
  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: Running A 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 the 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.

Was this article helpful?

Leave a Comment