How to Set Up a PHP Interpreter

Olorunfemi Akinlua Feb 15, 2024
  1. Install a PHP Interpreter
  2. Run PHP via Terminal and Interpreter
  3. Configure PHP Interpreter
  4. Use PHP Interpreter with IDEs
How to Set Up a PHP Interpreter

PHP is a scripting language created for web development and server-side development. And scripting languages are often interpreted at runtime rather than compiled, which is the case for PHP.

Therefore, for you to run PHP on your local machine or PC, you need the PHP interpreter. This interpreter is the engine we will install on our computers to run PHP code rather than on a remote server or instance.

This article explains how we can set up a PHP interpreter, configure it with our selected IDE, and how we may make use of sandboxes to run PHP interpreters online.

Install a PHP Interpreter

Depending on the OS, the installation process will differ.

You can check the detailed process of installing PHP on macOS here. For Linux, the step-by-step installation process by Geeks for Geeks is a great start.

For Windows, it is relatively simple, and you can follow these steps.

  1. Go to PHP for Windows Page.

  2. Select the Thread Safe option for x64 or x86, depending on the bit configuration of your system.

    VS 16 x64 Thread Safe

  3. Extract the zip file to the C:\php folder. You will have to make a new folder named php within the root directory of your Windows PC (C:\).

    Select the newly created PHP folder

  4. After extraction, you will find tons of files within the C:\php folder, but our main concern here is the php.exe (our PHP interpreter) file which should be in the main directory. Therefore, the path to our PHP interpreter is C:\php\php.exe.

    The PHP interpreter inside the php directory

  5. Now, set up the PHP path to your environment variable to allow you to use PHP from anywhere (directory) on your PC. To do that, search for Environment Variables.

    Searching for Environment Variables

  6. Select Environment Variables (highlighted).

    Select Environment Variables

  7. Edit the Path in the System Variables section.

    Edit Path Variable

  8. Click New to add the PHP path to the environment variable.

    Add PHP path to Environment Variable

  9. Add path and save changes.

    Add C:\php to environment variable

  10. Test if everything’s working properly; go to the Command Prompt, Windows PowerShell, or Windows Terminal, and type php -v. You’re good to go if you see the current PHP version.

    Checking the PHP version on the Windows PowerShell

Run PHP via Terminal and Interpreter

Now that you have PHP on your local PC and can run it natively, you can run the PHP code you have from the terminal using the following command.

php index.php

Let’s try an example code to set it out.

<?php

echo("Hello world\n");

$holder = [12, 34, 56];

foreach ($holder as $key) {
   echo($key);
   echo("\n");
}

?>

The output through the terminal will look like this:

Running code through the terminal

Also, you can easily accept inputs from the terminal rather than from a webpage. The cool thing is you can run a web server right from your terminal using the command below.

php -S localhost:8000 -t php/

The 8000 serves as the port; you can change it to any port number you wish, especially if another application already uses 8000. The php/ is the folder from which you serve your PHP code.

The output of the code is below.

PHP code running and rendered via the localhost server

This is the PHP code running and rendered via the localhost server.

The code we wrote earlier is rendered on a webpage. The image below shows the server in operation via the terminal.

Localhost in operation

Configure PHP Interpreter

After installing PHP on your PC, the configuration aspect will largely depend on the IDE you plan on using.

There are different IDEs out there for people to use. Some are paid, and some are free.

We can’t cover all of them, but we can point to you specific documentation that explains some. This article will discuss just two IDEs, VS Code and PHP Storm.

VS Code is the best IDE for almost every language and is free.

VS Code can run PHP natively and easily with built-in features and extensions. Same with PHP Storm, but it’s paid and has many professional contextual features for developers.

Use PHP Interpreter with IDEs

  1. Visual Studio Code

    To work with the PHP interpreter on VS Code, here’s a detailed how-to guide on exploring VS Code as your IDE for development from extensions to support tools.

  2. PHP Storm

    JetBrains has documentation on configuring your PHP development environment and local PHP interpreters.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn