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:
-
Open the Command Line:
-
On Windows, open Command Prompt (cmd) or PowerShell.
-
On Linux or macOS, open the Terminal.
-
-
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/
-
Run the PHP Script:
Execute the script by typing the php command followed by the script's filename.
Example:php script_name.php
-
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.