[Solved] PHP is not recognized as an internal or external command

Advertisement

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.

Offer
Hostinger Offer

You may face the issues:

‘php’ is not recognized as internal or external command,
operable program or batch file.

Something as in the below screenshot:

[Solved] PHP is not recognized as an internal or external command 1
PHP is not recognized as an internal or external command

This error occurs because the environmental path for the php.exe file does not exist.

You need to set the environment path into the Windows Operating System to make it work.

You are going to see:

Add Environment Path for php.exe Add Environment Path for php.exe

Step 1: Click on the Start menu from the Windows Operating System search for Environment Variables and then click on Environment Variables

[Solved] PHP is not recognized as an internal or external command 3
System Properties

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
Environment Variables and System Variables

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 6
Edit Environment Variable

Top ↑

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 8
Successfully executing the PHP command

Top ↑

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 10
The “Hello World” PHP Program Output

You may also learn:

Here are some related articles that you might find helpful:

  • MySql is not an internal or external command
    The error message “MySql is not an internal or external command” typically occurs when the system is unable to recognize the MySQL command. This issue can usually be resolved by adding the MySQL executable path to the system’s PATH environment variable.
  • How to Install PHP on Windows
    This article provides step-by-step instructions on how to install PHP on the Windows operating system.

Feel free to explore these articles for more information and detailed guides on PHP-related topics.

Top ↑

Frequently Asked Questions Frequently Asked Questions

What does it mean when I get the error message “PHP is not recognized as an internal or external command”? What does it mean when I get the error message “PHP is not recognized as an internal or external command”?

This error message means that your computer is not able to find the PHP executable file, which is necessary to run PHP scripts.

Top ↑

How can I fix the “PHP is not recognized” error message? How can I fix the “PHP is not recognized” error message?

There are several steps you can take to fix this error message, including:

1. Adding the PHP installation directory to your system’s PATH environment variable.
2. Make sure that the PHP executable file is located in the correct directory.
3. Reinstalling PHP to ensure that all necessary files are properly installed.

Top ↑

How do I add the PHP installation directory to my system’s PATH environment variable? How do I add the PHP installation directory to my system’s PATH environment variable?

You can add the PHP installation directory to your system’s PATH environment variable by following these steps:

1. Open the Start menu and search for “Environment Variables”.
2. Click on “Edit the system environment variables”.
3. Click on the “Environment Variables” button.
4. Under “System Variables”, scroll down until you find the “Path” variable and click on “Edit”.
5. Click on “New” and enter the path to your PHP installation directory (e.g. C:\PHP).
6. Click “OK” to close all the windows and save your changes.
7. That’s it 🙂

Top ↑

How do I know if PHP is installed on my computer? How do I know if PHP is installed on my computer?

You can check if PHP is installed on your computer by opening the command prompt and typing “php -v”.

This will display the version of PHP that is currently installed.

Top ↑

What are some common causes of the “PHP is not recognized” error message? What are some common causes of the “PHP is not recognized” error message?

Some common causes of this error message include:

1. The PHP installation directory is not added to the system’s PATH environment variable.
2. The PHP executable file is not located in the correct directory.
3. The PHP installation is incomplete or corrupted.
4. The PHP version is incompatible with your operating system.

One thought on “[Solved] PHP is not recognized as an internal or external command

Leave a Reply