I am trying to learn how to access PHP scripts from the command line (CLI)
Below is an image from my attempt, please help. I am running Windows 7
I am trying to learn how to access PHP scripts from the command line (CLI)
Below is an image from my attempt, please help. I am running Windows 7
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):
My Computer
iconProperties
Advanced system settings
from the left navAdvanced
tabEnvironment Variables
buttonSystem Variables
section, select Path
(case-insensitive) and click Edit
button;
) to the end of the string, then add the full file system path of your PHP installation (e.g. C:\Program Files\PHP
)OK
etc until all dialog boxes have disappearedAlternatively, 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>
For temporary use, try this: (benefit for use npm
or composer
)
> SET PATH=c:\wamp\bin\php\php5.3.29\;%PATH%
For test use this:
> echo %PATH%
c:\wamp\bin\php\php5.3.29\;c:\anothers\;
Now you can access to PHP
command:
> php -v
PHP 5.3.29 (cli)
Easy Way: Run CMD then type
C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\timenow.php"
Change the last part for a different script, and make sure you have the same version php folder as I do. (php5.4.3)
On 2020 and with Windows 10 there's an easier way to achieve this.
1- Download composer at https://getcomposer.org/Composer-Setup.exe
2- Install it as you install any program on windows and (important) when it asks you the path to PHP, browse thru the correct path (for example: c:/wamp/bin/php.exe)
FINNISHED! Close the cmd and reopen and you can run PHP on your command line and also COMPOSER (composer is actually very needed if you use PHP)
you can run cd before you try and run you command, or call your script like