Understand the Windows environment variables path

Advertisement

Introduction Introduction

The PATH is the system variable in the Windows Operating System.

It contains the executables files path to run them from the command line or Terminal window.

Note: Making changes to the system PATH variable is typically not necessary for computers running Windows or Mac OS X. If you sure about the executable files then you can do it.

Top ↑

Understanding the Concept Understanding the Concept

Most of us face some of the below issues while development.

Example 1: PHP Example 1: PHP

We know that PHP is a server-side scripting language. But, We can execute PHP script though the command link too.

So, Without time waste, Let’s execute the PHP script through the command line with the below 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 below code into the file test.php

<?php
echo 'Hello World';

Step 3: Open Command Prompt or terminal

Step 4: Type command cd c:\xampp\htdocs\examples

Step 5: Type command php test.php

You may see below error:

'php' is not recognized as an internal or external command,
 operable program or batch file.

Why? Why we get an error?

Because by default Windows operating system not have any inbuild php command.

Now, Before making the PHP command in working condition lets us understands a few things:

First, locate the executable PHP file called php.exe.

I have installed XAMPP, which is installed on location C:\xampp\

and the location of my php.exe file is in C:\xampp\php\php.exe

Okay.

Now, In Step 5 we were used command:

php test.php

Instead of above command try:

C:\xampp\php\php.exe test.php

you can see something like below:

c:\xampp\htdocs\examples>C:\xampp\php\php.exe test.php
Hello World 

Yup! we see that our text Hello World is now printed in the terminal window.

Now, Understand that the file php.exe execute the code from the PHP file test.php.

So, If we don’t have set the environment path then we can use the C:\xampp\php\php.exe test.php instead of php test.php.

But, Always using path C:\xampp\php\php.exe is more time-consuming?

So, Let’s add our executable PHP file path C:\xampp\php\ into the windows environment variable to make php command as a recognized command.

Top ↑

Set system environment variable Set system environment variable

Step 1: Search for “system environment variables” and click on it.

Step 2: Click “Environment Variables”. In the section System Variables,

Step 3: Select the “Path” from “System Variables” and click edit.

Step 4: Click “New” and add the path “C:\xampp\php\” and click “Ok”.

Step 5: Close or Reopen Command prompt window.

Check the below image for reference:

Windows Environment Path Flow
Windows Environment Path Flow

Now, Execute our PHP file test.php with below steps:

Step 1: Type command cd c:\xampp\htdocs\examples
Step 2: Type command php test.php

You can see something like below:

c:\xampp\htdocs\examples>php test.php
 Hello World

Now, We don’t need to use C:\xampp\php\php.exe.

Top ↑

Command Error Messages: Command Error Messages:

Below are the most common error messages while trying to use commands.

NOTE: All below error will fix if we set their executable files path into the window’s environment path.

Top ↑

PHP PHP

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

Top ↑

MySql MySql

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

Top ↑

Node Node

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

Top ↑

NPM NPM

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

Top ↑

WP WP

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: