To run PHP file from windows command line, you must have installed PHP in your windows system. Individually setup PHP environment is tedious and lengthy task in windows system. So we will use fully operational XAMPP environment which comes with PHP, MySQL and Perl. If you haven’t installed XAMPP web server then here is an article about how to install XAMPP in windows.
I assume that XAMPP environment is installed in your windows system. Now open windows command line and setup XAMPP path.
To open windows command line, Press (windows + R) and type “cmd” and hit enter. It will open windows command prompt shown as below.
Now type “php -v” to check php version. If this command works fine and is not produce any error then we can assume that php is working in windows command line. If above command produce error like this “php is not recognized as an internal or external command” then you have to set your xampp php path in system environment variable.
Check this tutorial to fix “php is not recognized as an internal or external command” error.
After adding php path to environment variable, restart the command prompt and run again “php -v” command and it will give you version of PHP language.
Now create a simple PHP file with name “test.php” and put below code in it and save it in in “(Your Drive Name):/xampp/htdocs” folder.
<?php echo "I am John Doe."; ?>
To run test.php file, hit “php test.php” command. it will execute php file and show the result of php code. Check below screenshot
I hope that you have understand that how to run PHP file using command line. Still if you have any questions then please let me know in comment. I will try my best to give you satisfied answer. Thank you very much. 🙂