I'm a PHP Developer by profession.
I'm using a Lenovo Ideapad laptop that runs on Windows 10 Home Single Language 64-bit Operating System
I've also installed XAMPP Control Panel v3.2.2 at location "C:\xampp" to execute PHP programs in a web browser on my machine.
The "php.exe" file is present at the location "C:\xampp\php".
The document root directory to save the PHP files is at the location "C:\xampp\htdocs".
I'm able to run the PHP programs that I created and saved in a directory C:\xampp\htdocs\html_playground by starting the XAMPP software(by double clicking on XAMPP shortcut present on my desktop) and entering the URL of a program file like this "http://localhost/html_playground/sample.php" in a browser's address bar.
This way I'm able to run the PHP programs finely but I want to run the same program from Windows Command Prompt
For it I done following steps :
The final new string was looking like below :
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps; C:\xampp\php
Then instead of showing the output of the program it opened the same file in Sublime Text(The editor I'm using to write the code)
The sample.php file has got the following PHP executable code :
<?php
echo 'Hello World!';
?>
For your reference I'm also attaching the screen-shot of the command prompt window :
Now my question is
Please somebody help me out by answering my queries and helping me in running the program from command prompt.
Even I tried restarting the PC and run the php -v command but it also didn't work out. Following is the screen-shot of the same :
If you're wanting to run CLI PHP I would highly suggest installing the vanilla PHP found on windows.php.net. Let it install to C:\php
The reason I recommend this is that if you ever want to use Composer (and there's a really good chance you will want to), it will natively find it there.
Now you can open a command prompt and run scripts like follows
C:\php\php.exe C:\path\to\php\script.php
To your specifics
Why I'm not able to see the output at command prompt or a web browser?
Because the output is sent to the command line. Browser requests are sent via the web server to the browserIs it necessary to start the XAMPP server like I normally do to run the program from command prompt too?
For CLI requests, no. PHP is an executable and can run on its own. If you want to do browser requests then XAMPP needs to be running.Is there really a need to set environment variables? If yes why? If no why?
Not really. The CLI will only pay attention to php.ini
XAMPP configs are for Apache, mostly.But I'm not understanding the reason why people do insist for running the PHP programs from Command Line
The main reason is automated tasks. I have several processes at work I run on a cron job. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With