Command Line Execute Code

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.

You are going to see:

Set-up Localhost Set-up Localhost

To execute the PHP on your computer you need the localhost environment.

You can install the:

  • Xampp
  • Mamp
  • Lamp
  • Local by Flywheel

If you already installed it then you can skip this step.

Top ↑

Check PHP is available in the command line Check PHP is available in the command line

To test the PHP is available with the command line.

  • Open Terminal (CMD)
  • Type php -v

You can see something similar on the screen.

Command Line Execute Code 1
Successfully executing the PHP command

If you see an error like:

'php' is not recognized as internal or external command, operable program or batch file.
Command Line Execute Code 2
PHP is not recognized as an internal or external command

Then see fix the PHP is not recognized as an internal or external command

Top ↑

Try PHP with the Hello World example Try PHP with the Hello World example

I’m using the XAMPP, So, giving an example of XAMPP.

You can use any localhost server.

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:

Command Line Execute Code 3
The “Hello World” PHP Program Output

Leave a Reply