We know that PHP is a server-side scripting language. But, We can execute PHP script though the command link too.
But, Most of the time we face an issue:
This is because of the environment path for the
php.exe file is not exist. To make it work you need to set the environment path into the system setting.
Table of Content
Add Environment Path Add Environment Path
Step 1: Click on Start menu from windows Operating System and search for Environment Variables and then click on Environment Variables
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 though command line.
- User variables – If we set the path for the current user then only current user can execute the PHP though command line.
Step 3: Add the PHP directory location where the PHP is installed. In XAMPP we can find the PHP location at:
So, Click on New, add the path, and click on Ok.
To test the PHP is available with command line.
- Open Terminal (CMD)
You can see something similar screen.
Now, Let’s see how to execute PHP script through the command line with the 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 below code into the file
<?php echo 'Hello World';
Step 3: Open terminal or CMD (Command Prompt) and navigate to the examples directory.
Step 4: Execute command
You can see the Hello World into the terminal screen like: