You know that PHP is a server-side scripting language.
But, You can execute the PHP script through the command prompt or terminal as well.
You may face the issue:
‘php’ is not recognized as internal or external command,
operable program or batch file.
Something as in below screenshot:
![[Solved] PHP is not recognized as an internal or external command 1 [Solved] PHP is not recognized as an internal or external command 1](https://maheshwaghmare.com/wp-content/uploads/2021/05/php-not-a-internal-or-external-command.png)
This error occurs because the environmental path for the php.exe
file does not exist.
To make it work you need to set the environment path into the operating system.
You are going to see:
![[Solved] PHP is not recognized as an internal or external command 2 [Solved] PHP is not recognized as an internal or external command 2](https://maheshwaghmare.com/wp-content/uploads/2023/01/image-14.png)
Add Environment Path for php.exe Add Environment Path for php.exe
Step 1: Click on the Start menu from Windows Operating System and search for Environment Variables and then click on Environment Variables
![[Solved] PHP is not recognized as an internal or external command 3 [Solved] PHP is not recognized as an internal or external command 3](https://maheshwaghmare.com/wp-content/uploads/2021/05/add-environment-variables.png)
Step 2: Edit the System variables or User Variables
- System variables – If we add a path in system variables then other system users also use the PHP through the command line.
- User variables – If we set the path for the current user then only the current user can execute the PHP through the command line.
![[Solved] PHP is not recognized as an internal or external command 4 [Solved] PHP is not recognized as an internal or external command 4](https://maheshwaghmare.com/wp-content/uploads/2021/05/add-new-path-screen.png)
Deploy your application to Kinsta – Start with a $20 Credit now. Deploy now and get $20 off
Step 3: Add the PHP directory location where the PHP is installed. In XAMPP we can find the PHP location at:
c:\xampp\php
So, Click on New, add the path, and click on Ok.
![[Solved] PHP is not recognized as an internal or external command 5 [Solved] PHP is not recognized as an internal or external command 5](https://maheshwaghmare.com/wp-content/uploads/2021/05/add-new-environment-path.png)
How to confirm the issue is fixed? How to confirm the issue is fixed?
To test the PHP is available with the command line.
- Open Terminal (CMD)
- Type
php -v
You can see something similar on the screen.
![[Solved] PHP is not recognized as an internal or external command 6 [Solved] PHP is not recognized as an internal or external command 6](https://maheshwaghmare.com/wp-content/uploads/2021/05/php-version-check-with-cli.png)
Struggling with downtime and WordPress problems? Kinsta is the hosting solution designed to save you time! Check out our features
Try with a small example Try with a small example
Now, Let’s see how to execute the PHP script through the command line with a simple example.
Note: I assume that you have a localhost server XAMPP, MAMP, WAMP, VVV, Local By Flywheel, or something else.
Step 1: Create a file test.php
in c:\xampp\htdocs\examples
Step 2: Copy and paste the below code into the file test.php
<?php echo 'Hello World';
Step 3: Open a terminal or CMD (Command Prompt) and navigate to the examples directory.
cd c:\xampp\htdocs\examples
Step 4: Execute the command
php test.php
You can see Hello World on the terminal screen like:
![[Solved] PHP is not recognized as an internal or external command 7 [Solved] PHP is not recognized as an internal or external command 7](https://maheshwaghmare.com/wp-content/uploads/2021/05/execute-php-command-line.png)
You may also learn:
Thanks , examples about command line programs are less on the web.