I. Introduction

In this blog post, I’ll show you how to run PHP in Visual Studio Code terminal using the CodeRunner extension.

PHP is a widely-used programming language that is especially suited for web development. It is often used in conjunction with other technologies like HTML, CSS, and JavaScript to create dynamic and interactive websites.

Visual Studio Code (VS Code) is a popular choice for PHP development due to its powerful features and wide range of extensions. One of the most powerful features of VS Code is its in-built terminal that allows developers to execute shell commands right within their IDE.

It is pretty easy to run PHP code inside VS Code by following these five steps:

  1. Ensure You Have PHP Installed in your machine
  2. Configure VS Code to recognize PHP as a language
  3. Install Code Runner Extension
  4. Configure Code Runner Extension
  5. Run Your PHP Code in VS Code

Let’s break down each of these steps.

II. Step-By-Step Guide

Step 1: Ensure You Have PHP Installed in your machine

Before you can start using PHP in VS Code, you’ll need to have PHP installed on your computer. I would recommend installing nothing less than PHP 8.0.1. This is because PHP 7.4 recently reached its End Of Life on November 28th, 2022.

To confirm php is installed on your machine, open terminal and run the following command.

php -v

You should get an output as below showing the PHP version installed.

PHP 8.1.12 (cli) (built: Oct 30 2022 12:39:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies

If you have the output above, proceed to Step 2.

If you get an error in this step, you must install PHP. Installing PHP on your computer will depend on the operating system in use.

How to Install PHP On Your Laptop?

A. How to Install PHP on Mac

For Mac users, I recommend using HomeBrew to install PHP on your machine and for almost any other software you will ever need to install.

Open your terminal and paste the below command:

brew install php

This will install the latest version of PHP, which at the time of writing is version 8.2.

To install a specific version of PHP, use the below command and substitute 8.0 with the respective version of PHP you want.

brew install php@8.0
B. How to Install PHP on Windows

To install PHP on your windows laptop, you can use either of these two ways:

  1. Download and Instal PHP from the official website
  2. Download and Install XAMPP

Step 2: Configure VS Code to recognize PHP as a language

Once you have it installed, you’ll need to configure VS Code to recognize PHP as a language.

This can be done by adding the following line to your settings.json file:

"files.associations": { "*.php": "php" }

Step 3: Install Code Runner Extension

The next step is to install the CodeRunner extension for VS Code. This extension allows you to run PHP code directly from within VS Code.

You can install CodeRunner from the VS Code Extension Marketplace.

Step 4: Configure Code Runner Extension to run PHP Code in Vs Code

Once the extension is installed, we need to configure it to execute our PHP code within the VS Code Terminal.

To do so, we will add the PHP Executable File Path to settings.json under CodeRunner settings.

A. How to Configure Code Runner on Mac/Linux/Unix
  1. Open your settings.json file by pressing  + shift + P 
  2. Paste the code below at the end

If you installed PHP using Homebrew:


"code-runner.executorMap": {
   
        "php": "/opt/homebrew/bin/php",
    }

Other PHP Installations(Linux/Unix):


"code-runner.executorMap": {
   
        "php": "/usr/bin/php",
    }
B. How to Configure Code Runner on Windows
  1. Open your settings.json file by pressing either F1 or Ctrl+Shift+P
  2. Paste the code below at the end.

If you installed PHP using XAMPP:

"code-runner.executorMap": {
   
        "php": "C:\\xampp\\php\\php.exe",
    }

If you installed PHP from the Official Website:

"code-runner.executorMap": {
   
        "php": "C:/php/php.exe",
    }
  1. On Your Windows Search, open Control Panel
  2. Click the Advanced System Settings
  3. Click on Environment Variables
  4. In the New System Variable window, add PHP Executable File Path to the value of the PATH environment variable
  5. Close and Reopen all Command prompt windows
Add PHP PATH to Environment Variables

Step 5: Run Your PHP Code in Visual Studio Code

With these steps done, you are ready to run your PHP code using the in-built VS Code terminal using Code Runner.

CodeRunner can execute your PHP code using either of these prompts:

  • Right-click from within the VS Code Text Editor and click Run Code.
Right Click to access Code Runner
  • Press Ctrl + Alt + N

To test it out, you can copy this simple PHP script.

<?php
// echo TODAY's Date:
$date = date('Y-m-d');
$time = date('H:i');
echo "Echo Foxtrot, Today's date is " . $date. " and the time is". $time ;

?>

Your output should be as below.

Code Runner PHP Code Output

III. Conclusion

In this blog post, I have shown you how to run your PHP code in VS Code terminal using the CodeRunner extension. We’ve covered how to set up PHP and configure VS Code, how to install and use the CodeRunner extension.

By following these steps, you’ll be able to take advantage of the powerful features of VS Code and CodeRunner to develop and debug your PHP code.

Related: How To Run Javascript Code in VS Code Terminal

Share.

Comments are closed.

Exit mobile version