Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a PHP program from command prompt on a Windows Machine?

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 :

  • Went to Went to Advanced System Settings (Control Panel\System and Security\System\Advanced System Settings)
  • Then clicked on Environment Variables
  • Then selected the variable Path
  • Then clicked Edit... button
  • Then after the ending semicolon of existing string I added the string "C:\xampp\php" by adding a blank space after the semicolon.

The final new string was looking like below :

%USERPROFILE%\AppData\Local\Microsoft\WindowsApps; C:\xampp\php

  • Then clicked on Ok
  • Opened command prompt
  • Went to the path C:\xampp\htdocs\html_playground on command prompt using cd command
  • Typed in sample.php(The file contaning my PHP program)

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 :

Command Prompt Window

Now my question is

  • Why I'm not able to see the output at command prompt or in a web browser?
  • Am I doing any mistake or what?
  • Is it necessary to start the XAMPP server like I normally do to run the program in a web browser for executing the program from command prompt too?
  • Did I make any mistake in setting environment variables?
  • Is there really a need to set environment variables? If yes why? If no why?
  • As PHP is supposed to be the suitable language for web development then I think it's always good to run the program in web developer's software i.e. a Web Browser. But I'm not understanding the reason why people do insist for running the PHP programs from Command Line rather than running the same program from web browser only?

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 :

Command Prompt output after system restart

like image 398
PHPFan Avatar asked Nov 27 '22 13:11

PHPFan


1 Answers

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

  1. 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 browser
  2. Is 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.
  3. 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.
  4. 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.
like image 179
Machavity Avatar answered Dec 04 '22 14:12

Machavity