How to access PHP with the Command Line on Windows?
You need to add your PHP installation directory to the %PATH%
environment variable, or work from the PHP installation directory.
To add it to path (The best approach - Edited for Windows 7):
- Right-click on a
My Computer
icon - Click
Properties
- Click
Advanced system settings
from the left nav - Click
Advanced
tab - Click
Environment Variables
button - In the
System Variables
section, selectPath
(case-insensitive) and clickEdit
button - Add a semi-colon (
;
) to the end of the string, then add the full file system path of your PHP installation (e.g.C:\Program Files\PHP
) - Keep clicking
OK
etc until all dialog boxes have disappeared - Close your command prompt and open it again
- Sorted
Alternatively, you can run cd <PHP installation path>
before you try and run you command, or call your script like <FULL file system path of php.exe> <path to script>
how to access the command line for xampp on windows
You can set environment variables as mentioned in the other answers (like here)
or
you can open Start > CMD as administrator and write
C:\xampp\php phpfile.php
Run .php file in Windows Command Prompt (cmd)
If running Windows 10:
- Open the start menu
- Type
path
- Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.
If on older Windows:
Show Desktop.
Right Click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the System Properties window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php - and download the installation file from there.
Find PHP version on windows command line
In command prompt type below
set PATH=%PATH%;C:\path\to\php
Where C:\path\to\php is the folder where your php.exe
file is located. After this run
C:\>php -v
than you will see something like
PHP 7.3.6 (cli) (built: May 29 2019 12:11:00)
UPDATE:
If you find that when you restart your computer, this solution will not work anymore:
Set the Path like as following:
Step 1 - Click on the
Windows
icon
Step 2 - Click on the
Settings
icon
Step 3 - Click on
System
Step 4 - Click on
About
Step 5 - Click on
System info
Step 6 - Click on
Advanced system settings
Step 7 - Click on
Environment variables...
Step 8 - Select
Path
row and then clickEdit
Step 9 - Click
New
and then clickBrowse
, then in the next panel which will open you need to select the folder you want in thePath
. For the initial premise of this guide i will add the folderC:\Users\NewFolderInPath
♥Note: Replace your PHP installed path(e.g:
C:\Program Files\php
) with the above path ♥.
Step 10 - Click
OK
and click everyOK
button you will encounter to close every previous windows.
How to execute PHP globally on Windows CMD
In order to run php globally, make sure you have the php cli
set up properly in the environment variables.
if you wish to make it as simple as entering the name and accessing it, and want to try something a bit different as mentioned in the other answer, do this.
Compile this program in c++ to make an executable Jimboo.exe
and add the location of Jimboo.exe
to your PATH. Then only typing in Jimboo
in cmd will execute the command.
#include<iostream>
using namespace std;
int main()
{
cout << "Running Jimboo";
system("php yourPHPfile.php argument1 argument2 ... ");
}
make sure you add php to PATH before using the above code.
Related Topics
How to Read Last Lines (I.E. "Tail") from a File Using PHP
How to Remove Extension from String (Only Real Extension!)
How to Get the Sqlsrv Extension to Work With PHP, Since Mssql Is Deprecated
How to Add Exif Data to an Image
Should a Mamp Return ::1 as Ip on Localhost
Is There an Equivalent For Var_Dump (PHP) in JavaScript
How to Get Last Key in an Array
What's the Difference Between ++$I and $I++ in PHP
How to Get Enum Possible Values in a MySQL Database
Insert Query on Page Load, Inserts Twice
Alternative to MySQL_Real_Escape_String Without Connecting to Db