title: How to run a PHP program from command prompt on a Windows Machine?
body: <p>I'm a <strong>PHP Developer</strong> by profession.</p>
  
  <p>I'm using a <strong>Lenovo Ideapad laptop</strong> that runs on <strong>Windows 10 Home Single Language 64-bit Operating System</strong></p>
  
  <p>I've also installed <strong>XAMPP Control Panel v3.2.2</strong> at location <strong>"C:\xampp"</strong> to execute <strong>PHP programs</strong> in a <strong>web browser</strong> on my machine.</p>
  
  <p>The <strong>"php.exe"</strong> file is present at the location <strong>"C:\xampp\php"</strong>.</p>
  
  <p>The document root directory to save the PHP files is at the location <strong>"C:\xampp\htdocs"</strong>.</p>
  
  <p>I'm able to run the PHP programs that I created and saved in a directory <strong>C:\xampp\htdocs\html_playground</strong> by starting the <strong>XAMPP</strong> software(by double clicking on XAMPP shortcut present on my desktop) and entering the URL of a program file like this <strong>"<a href="http://localhost/html_playground/sample.php" rel="nofollow noreferrer">http://localhost/html_playground/sample.php</a>"</strong> in a browser's address bar.</p>
  
  <p>This way I'm able to run the PHP programs finely but I want to run the same program from <strong>Windows Command Prompt</strong></p>
  
  <p>For it I done following steps :</p>
  
  <ul>
  <li>Went to Went to <strong>Advanced System Settings</strong> (Control Panel\System and Security\System\Advanced System Settings)</li>
  <li>Then clicked on <strong>Environment Variables</strong> </li>
  <li>Then selected the variable <strong>Path</strong> </li>
  <li>Then clicked <strong>Edit...</strong> button </li>
  <li>Then after the ending semicolon of existing string I added the string <strong>"C:\xampp\php"</strong> by adding a <strong>blank space</strong> after the semicolon. </li>
  </ul>
  
  <p>The final new string was looking like below :</p>
  
  <p><strong>%USERPROFILE%\AppData\Local\Microsoft\WindowsApps; C:\xampp\php</strong></p>
  
  <ul>
  <li>Then clicked on <strong>Ok</strong></li>
  <li>Opened <strong>command prompt</strong></li>
  <li>Went to the path <strong>C:\xampp\htdocs\html_playground</strong> on command prompt using <strong>cd</strong> command</li>
  <li>Typed in <strong>sample.php</strong>(The file contaning my PHP program)</li>
  </ul>
  
  <p>Then instead of showing the output of the program it opened the same file in <strong>Sublime Text</strong>(The editor I'm using to write the code)</p>
  
  <p>The <strong>sample.php</strong> file has got the following <strong>PHP executable code</strong> :</p>
  
  <pre><code>&lt;?php 
     echo 'Hello World!';
  ?&gt;
  </code></pre>
  
  <p>For your reference I'm also attaching the screen-shot of the command prompt window :</p>
  
  <p><a href="https://i.stack.imgur.com/Kfefy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kfefy.png" alt="Command Prompt Window"></a> </p>
  
  <p>Now my question is </p>
  
  <ul>
  <li>Why I'm not able to see the output at command prompt or in a web browser?</li>
  <li>Am I doing any mistake or what? </li>
  <li>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? </li>
  <li>Did I make any mistake in setting environment variables? </li>
  <li>Is there really a need to set environment variables? If yes why? If no why?</li>
  <li>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 <strong>Command Line</strong> rather than running the same program from web browser only? </li>
  </ul>
  
  <p>Please somebody help me out by answering my queries and helping me in running the program from command prompt.</p>
  
  <p>Even I tried restarting the PC and run the <strong>php -v</strong> command but it also didn't work out. Following is the screen-shot of the same :</p>
  
  <p><a href="https://i.stack.imgur.com/PkEn5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PkEn5.png" alt="Command Prompt output after system restart"></a></p>