How to Execute a PHP Script from the Command Line | PHP CLI Guide

How to Execute a PHP Script from the Command Line | PHP CLI Guide

Category: Php

Executing a PHP script from the command line is a simple and efficient process using the PHP CLI (Command-Line Interface). This method allows you to run PHP scripts directly without needing a web server, making it ideal for tasks such as automation, cron jobs, or debugging.

✅ Steps to Execute a PHP Script from the Command Line:

  1. Open the Command Line:

    • On Windows, open Command Prompt (cmd) or PowerShell.

    • On Linux or macOS, open the Terminal.

  2. Navigate to the Script Location:
    Use the cd command to navigate to the folder where your PHP script is stored.
    Example:

    cd /path/to/your/script/

  3. Run the PHP Script:
    Execute the script by typing the php command followed by the script's filename.
    Example:

    php script_name.php

  4. View the Output:
    Any output generated by the script (such as echo statements) will be displayed directly in the command line.

🧠 Example:

php myscript.php

This will execute the PHP script myscript.php located in the current directory.

  • Difference between isset() and empty() in PHP?
  • What are Magic Methods in PHP?
  • What is the difference between unset() and unlink() in PHP?
  • What is the difference between MySQLi and PDO?
  • What is the difference between == and ===in