Run PHP Scripts from the Command Prompt in Any Folder
This article will teach you how to download and run PHP scripts from Command Prompt on PCs. It will also show you how to run PHP in any folder, not just the folder containing your php.exe file.
Contents
Steps
Downloading and Installing
- Download a "Thread Safe" version of PHP that corresponds to your computer. For example, if you have a 64-bit computer, download: Zip [23.3MB] VC14 x64 Thread Safe (pictured above).
- Navigate to "Downloads" folders, right click on the downloaded folder and select Extract All.
- Select Browse and choose the folder where you would like the extracted files to be nested. Note that in this scenario, we are extracting the files to a new folder we have created on the desktop titled “PHP” for ease of access.
Setting Up PHP To Run In Any Folder
- Navigate to the folder with the extracted files and single left click on the file titled “php”.
- Right click on the navigation bar and select Copy address as text.
- Open Control Panel and type "environment" in the search bar. When the search results populate, select Edit environment variables for your account.
- Under “User variables”, select “Path” and click Edit. If the variable “Path” does not exist, click New under “User variables”, and in “variable name”, type “Path”.
- Right click under the variable name and click Paste. You should now see the file address in the variable value section.
- If a variable value already exists, scroll the end of the existing value, input a semicolon, and paste the file address to the right of the semicolon
- Select OK in the “Edit User Variable” window and then select OK in the “Environment Variables” window.
Running PHP In Command Prompt
- Locate a .php file to test that everything is functioning correctly. Notice that you should now be able to run PHP from Command Prompt in any folder. The above picture is an example of a simple .php file which displays the message "Hello, World!"
- Open Command Prompt. In this scenario, we have placed “helloworld.php” file on the desktop to test its functionality.
- Open Command Prompt and navigate to wherever you have placed your test .php file.
- Type “php {Insert your test file name here}.php”. If everything worked as it should, your .php file should run. In this case, Command Prompt displayed "Hello, World!"
Things You'll Need
- Computer with Windows operating system (preferably Windows 7 or higher)
Sources and Citations
- The PHP Group (2017). PHP 7.1 (7.1.6). Retrieved from http://windows.php.net/download#php-7.1