Open In App

How to Execute PHP Code using Command Line?

Last Updated : 27 Jun, 2025
Suggest changes
Share
Like Article
Like
Report

PHP is mainly used for creating dynamic websites, but you can also run PHP scripts without a web server. This can be done using the PHP Command Line Interface (PHP CLI), which allows you to execute PHP scripts directly from the command line. This method is commonly used for testing, automation, or running background tasks by using the php command followed by the script name or inline code.

To execute PHP code using the command line, you can use several methods. Here's a guide with the most common ones:

Step 1: Installation of PHP

First, you need to install PHP on your system. Once PHP is installed, you can use the command line to execute PHP scripts using the PHP Command Line Interface (CLI). This allows you to run PHP code directly without needing a web server.

  • On Windows: Follow the step-by-step guide to install PHP from GeeksforGeeks.
  • On Linux: Check out the detailed installation steps for PHP on Linux through the GeeksforGeeks guide.
  • On Mac: Learn how to install PHP on macOS from the official documentation.

Step 2: Executing PHP code using the command line

After the successful installation of PHP in your system, you can now start the PHP code execution using the command line. It means that you are running PHP scripts or code directly in the terminal or command prompt without a web server.

To execute PHP code using the command line, follow these simple steps:

1. Execute a PHP Script File

  • Create a PHP File (e.g., script.php):
PHP
<?php
  echo "Hello, World!";
?>
  • Open your terminal or command prompt.
  • Navigate to the directory containing the file.
  • Run the script using the php command:
PHP
<?php
   php script.php
?>

2. Execute PHP Code Inline Using -r

  • You can execute a small PHP code snippet directly from the command line without needing a script file.
PHP
<?php
    php -r 'echo "Hello from the command line!";'
?>

3. Passing Arguments to PHP Script

  • You can pass arguments to your script using the $argv array.
  • Example PHP file (args.php):
PHP
<?php
  echo "Argument 1: " . $argv[1] . "\n";
  echo "Argument 2: " . $argv[2] . "\n";
?>
  • Run the script with arguments:
PHP
<?php
    php args.php value1 value2
?>

4. Interactive Mode (php -a)

  • To run PHP code interactively, use the -a option.
PHP
<?php
    php -a
?>

5. Running PHP Scripts in the Background

  • If you need to run a PHP script in the background (e.g., for long-running processes), append & to the command:
PHP
<?php
    php script.php &
?>
  • This will run the script in the background and free up the terminal.

6. Running PHP with Built-in Web Server (php -S)

  • PHP includes a built-in web server that you can use for testing purposes. This allows you to run a local server without a full web server setup.
PHP
<?php
    php -S localhost:8000
?>
  • This starts a PHP server on localhost:8000. You can access it in your browser at http://localhost:8000.

Note:

  • Always use the complete path to your files to avoid mistakes, especially when running scripts from different folders.
  • Enable error reporting while testing to easily find any problems. Add ini_set('display_errors', 1) in your script.
  • For scripts that run for a long time, save the output to a log file using error_log() or file_put_contents() so you can check it later.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


Next Article

Similar Reads