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

16,391

Solution 1

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.

Solution 2

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

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

Solution 4

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.

Solution 5

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.

Share:
16,391
PHPFan
Author by

PHPFan

Updated on June 14, 2022

Comments

  • PHPFan
    PHPFan almost 2 years

    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