4

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

PHPLover
  • 1
  • 51
  • 158
  • 311
  • you need to run `php sample.php` (you need to actually start the program first, then feed it a file to parse). And you should be able to do this from anywhere, although you then need to put the full path to the file, so (from c:) `php C:\xampp\htdocs\html_playground\sample.php` – junkfoodjunkie Apr 14 '17 at 15:59
  • @junkfoodjunkie : Tried but didn't work. See the updated question with output screenshot. – PHPLover Apr 14 '17 at 16:07
  • did you restart you pc after adding in the php enviroment variable? After it starts open the command prompt and type php -v to see if it works. – dsadnick Apr 14 '17 at 16:18
  • @dsadnick : Tried restart but that also didn't work. See the second screen-shot of the same from my updated question. – PHPLover Apr 14 '17 at 16:25
  • Are you sure the file is actually called just php? – junkfoodjunkie Apr 14 '17 at 16:47
  • It's `php.exe`, but just `php` works fine. You shouldn't actually put spaces into the environment variable, not sure if that's the problem though. – Charlotte Dunois Apr 14 '17 at 16:51
  • As @CharlotteDunois said, you shouldn't put spaces in your environment variable. Secondly, check if your edit was correctly saved by doing `echo %PATH%` in your windows CLI. Then you may have noticed that if you want the CLI to look for your PHP (e.g full path of php.exe: C:\xampp\bin\php.exe) so you have to include C:\xampp\bin in your path variable. Hope it will help. – Joffrey Carle Apr 18 '17 at 15:22

7 Answers7

12

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.
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • If I'm using WAMP how can I make Composer detect that folder (c:\ php) In my system? – Jorge Peña May 10 '17 at 19:51
  • If you are running WAMPServer [see this answer for another option](http://stackoverflow.com/questions/15597067/how-to-run-php-from-windows-command-line/16289254#16289254) that uses one or ANY of the PHP versions that WAMPServer allows you to have on the system at any one time – RiggsFolly May 10 '17 at 20:13
  • @JorgePeña Hmm, apparently the Windows installer added `C:\php` as a PATH after all. Either way, keeping the base installer for CLI is cleaner in the long run (things get messy when upgrading WAMP stacks as they play with paths) – Machavity May 10 '17 at 20:21
10

Step by Step guide for variable paths in Windows 10


Update:

Skip to Step 4 by left clicking the Windows Start Button and then immediately type system env, windows search will suggest Edit the system environment variables, Click this and and go to step 4.


  1. Right Click Windows Start Button, Select System

Right Click Windows Start Button > System

  1. Click on System Information

Click on System Information

  1. Click on Advanced System Settings

Click on Advanced System Settings

  1. Click on Environment Variables

Click on Environment Variables

  1. Select PATH under System Variables heading then click Edit.

Under System Variables, Click on PATH then click Edit

  1. Click New, Type in Path location for executable file

Click New, Add Path location to executable file

  1. Press OK and APPLY on all applicable panels then restart the computer.

enter image description here

Marc
  • 5,109
  • 2
  • 32
  • 41
3

Step 1: Right click on the Computer icon on your Desktop and choose Properties option.

Step 2: In the System window click on Advanced system settings in the left pane

Step 3: In the System Properties window select Advanced tab from horizontal menu at top and click on Environment Variables button given at the bottom of the window

Step 4: In the next window call Environment Variables, go to the System variable (second grid) and find a variable named Path and click Edit. If the Path variable is not present then click to New.

If you are editing then append a semicolon before the path you are adding to separate variables.

Step 5: Get the php path where you have installed XAMPP. Add this path to variable value in little dialog box that appeared.

Step 6: Press OK and close all windows.

Step 7: open a command prompt with cmd.

Step 8: cd to your_install_path/xampp/htdocs

Step 9: enter the command 'php -i | more'

Step 10: There is no step 10.

Bruce
  • 1,039
  • 1
  • 9
  • 31
2

Since you have installed XAMPP then it is possible for you to run PHP from the SHELL window of XAMPP Control Panel v3.2.2

enter image description here

In my test-case i use a small file index.php inside directory c:\xampp\htdocs\tester the content of index.php is:

echo "This is output from command line "."\n";
if(!empty($argv)) {
foreach($argv as $key => $arg) {
if($key>0) {
echo "command line argument ".$key ." ". $arg."\n";
}
}
}

Simply type the following in your command line window.

php C:\xampp\htdocs\tester\index.php var1=1 var2="lorem ipsum..."

Output

enter image description here

2

You can see php code output in your browse via using the -S flag:

php -S <host/ip>:<port>

To test it, create an sample.php file containing:

<?php
echo "Hello World from built-in PHP server";
?>

and run the command php -S localhost:8080 from the command line (do not include http://).

This will start a web server listening on port 8080 using the current directory that you are in as the document root.

Open the browser and navigate to http://localhost:8080. You should see your 'Hello World' page.

Note: My assumption is that you are in which directory where sample.php present. If in case port:8080 are already in using you can use any other port like 80, 8000.

Asif Raza
  • 3,435
  • 2
  • 27
  • 43
1

Open a command prompt in the same directory as php.exe and call the program with the argument of the file you wish to process.

C:\xampp\php\php.exe C:\xamp\htdocs\html_playground\test.php

If test.php contained; <?php echo "Hello World"; ?> then the command prompt would output 'Hello World' and return standard CMD CLI user input.

The reason of your >php call not working is more than likely due to your PATH environment variable not containing the correct location for PHP, edit your system variables and add 'C:\xampp\php\;' into the PATH string. This should then allow you to call php C:\xamp\htdocs\html_playground\test.php for the same output as above.

Dan_
  • 1,235
  • 11
  • 20
-2

Simple, Short and Perfect answer for You. Try at least one time you will get Success :)

Open C:\xampp\php (or your path may be different depends where you installed it)

Now run PHP.exe

now just follow this type

php -a 

and then start writing your code

for example

<?php 
echo "Hello, world!"; 
?> 

Then Press CTRL+Z and then Enter

Hello, world! // Output

rohitcoder
  • 401
  • 1
  • 7
  • 18