How to Set Up a PHP Interpreter
- Install a PHP Interpreter
- Run PHP via Terminal and Interpreter
- Configure PHP Interpreter
- Use PHP Interpreter with IDEs
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.
-
Go to PHP for Windows Page.
-
Select the
Thread Safe
option forx64
orx86
, depending on the bit configuration of your system. -
Extract the zip file to the
C:\php
folder. You will have to make a new folder namedphp
within the root directory of your Windows PC (C:\
). -
After extraction, you will find tons of files within the
C:\php
folder, but our main concern here is thephp.exe
(our PHP interpreter) file which should be in the main directory. Therefore, the path to our PHP interpreter isC:\php\php.exe
. -
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
. -
Select
Environment Variables
(highlighted). -
Edit the
Path
in theSystem Variables
section. -
Click
New
to add the PHP path to the environment variable. -
Add path and save changes.
-
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.
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:
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.
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.
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
-
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.
-
PHP Storm
JetBrains has documentation on configuring your PHP development environment and local PHP interpreters.
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