How to Set Environment Variable in PHP

Habdul Hazeez Feb 02, 2024
How to Set Environment Variable in PHP

This tutorial is an illustrated step-by-step guide on setting up an environment variable for PHP using WAMP on your Windows system. This allows you to use PHP from the command line.

Steps on How to Set the Environment Variable for PHP in Windows

With that said, the following are the steps to set up an environment variable for PHP.

  • Initial setup
  • Launch system settings
  • Launch Advanced system settings
  • Launch the Environment Variables dialog window
  • Create a new User variable
  • Test PHP from the command line

Initial Setup to Set the Environment Variable for PHP in Windows

To follow this tutorial, ensure you have WAMP installed on your system in your C:\ drive. Preferably, the installation folder for WAMP should be c:\wamp64 or c:\wamp32, depending on your system architecture and the version of WAMP you have installed.

Launch System Settings to Set the Environment Variable for PHP in Windows

You can launch the system settings from the Control Panel or via a shortcut from the Run dialog box; we’ll go with the latter option.

Press the Windows+R key simultaneously on your keyboard; this should bring up the Run dialog box, then type control system and hit Enter or click the OK button.

Windows Run dialog box

Launch Advanced System Settings to Set the Environment Variable for PHP in Windows

If you are using an earlier version of Windows, you’ll find the Advanced system settings link on the left side of the System Settings window. However, if you are using a modern version of Windows, it will be on the right side.

Clicking on the Advanced system settings link pops up a dialog window.

System Settings in Windows 10

Launch the Environment Variables Dialog Window to Set the Environment Variable for PHP in Windows

In the Advanced system settings dialog window, you’ll find a button close to the bottom of the dialog window. Click on the button that reads Environment Variables, as shown in the image below.

When you click on the button, you are presented with another dialog window to manage User variables and System variables in your system.

System Variables dialog box in Windows 10

Create a New User Variable to Set the Environment Variable for PHP in Windows

The dialog window for the Environment Variables has two sections; the first section is for setting the User variables, while the second section is for the System variables. Our interest lies in the first section.

Click on Path in the first section, then press the New button. Another dialog window pops up to create a New User Variable.

Environment Variables Dialog window in Windows 10

Type Path and the variable value in the variable name, click on the button that reads Browse Directory and navigate to the PHP directory in your WAMP installation.

New User Variable dialog window in Windows 10

If you installed the latest version of WAMP, you could have multiple versions of PHP installed. If that is the case, navigate to the folder of your preferred version of PHP and click the OK button.

Browse For Folder dialog box in Windows 10

The window to create a New User Variable has the variable name, Path and your PHP installation directory. With that, click the OK button to save, and you are set to use PHP from the command line.

New User Variable with filled data of PHP installation directory

Test PHP From the Command Line in Windows

Launch the command line and type php -v; this will display the version you’ve set in the system Environment Variables. If you get the PHP version, that means you can use PHP from the command line.

Checking PHP version via the command line

You can also check the PHP installation information, as shown in the image below.

Checking PHP installation information via the command line

Habdul Hazeez avatar Habdul Hazeez avatar

Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.

LinkedIn

Related Article - PHP Variable