How to Access PHP With the Command Line on Windows

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, select Path (case-insensitive) and click Edit 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:

  1. Open the start menu
  2. Type path
  3. Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.

If on older Windows:

  1. Show Desktop.

  2. Right Click My Computer shortcut in the desktop.

  3. Click Properties.

  4. You should see a section of control Panel - Control Panel\System and Security\System.

  5. Click Advanced System Settings on the Left menu.

  6. Click Enviornment Variables towards the bottom of the System Properties window.

  7. Select PATH in the user variables list.

  8. Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.

  9. Click OK

  10. Open your "cmd"

  11. Type PATH, press enter

  12. 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

Sample Image

Step 2 - Click on the Settings icon

Sample Image

Step 3 - Click on System

Sample Image

Step 4 - Click on About

Sample Image

Step 5 - Click on System info

Sample Image

Step 6 - Click on Advanced system settings

Sample Image

Step 7 - Click on Environment variables...

Sample Image

Step 8 - Select Path row and then click Edit

Sample Image

Step 9 - Click New and then click Browse, then in the next panel which will open you need to select the folder you want in the Path. For the initial premise of this guide i will add the folder C:\Users\NewFolderInPath

Note: Replace your PHP installed path(e.g:C:\Program Files\php) with the above path ♥.

Sample Image

Step 10 - Click OK and click every OK button you will encounter to close every previous windows.

Sample Image

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



Leave a reply



Submit