← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 9 of 10

PHP Command Line (CLI)

The PHP command line lets you run PHP scripts straight from a terminal window instead of through a web page. It is handy for chores like cleanup tasks and quick tools.
Syntax
php
// php script.php argument1 argument2
$script_name = $argv[0];
$argument = $argv[1];

$line = fgets(STDIN);   // read a line typed by the user

What is PHP CLI?

PHP CLI (Command Line Interface) runs a .php script directly from a terminal instead of routing it through a web server request — the right tool for background jobs, scheduled cron tasks, and one-off maintenance scripts that have no browser involved and don't need to produce HTML at all.

Example: What is PHP CLI?

php
<?php
// Print "Running via CLI: no HTTP request, no HTML needed\n" to the output
echo "Running via CLI: no HTTP request, no HTML needed\n";
// Print `php_sapi_name()` to the output
echo php_sapi_name();
?>

Command Line Arguments

Every argument you type after the script's filename on the command line lands in the built-in $argv array, with $argv[0] always holding the script's own filename first. $argc gives you the total count, which is handy for validating that the expected number of arguments was actually supplied before the script proceeds.

Example: Command Line Arguments

php
<?php
// Simulated since no args are passed in this sandbox: php script.php foo bar
$argv = ['script.php', 'foo', 'bar'];
$argc = count($argv);
echo "Script: $argv[0], Arg count: $argc\n";
for ($i = 1; $i < $argc; $i++) {
    echo "Arg $i: $argv[$i]\n";
}
?>

Interactive CLI Input

Calling fgets(STDIN) pauses script execution and waits for the person running it to type a line and press Enter, which is what turns a script from a fire-and-forget tool into something that can prompt for confirmation or collect a value interactively mid-run.

Example: Interactive CLI Input

php
<?php
// Print "Enter your name: " to the output
echo "Enter your name: ";
// Declare `$name`, set to `trim(fgets(STDIN))`
$name = trim(fgets(STDIN));
// Print `$name === '' ? "No input received" : "Hello, $name!"` to the output
echo $name === '' ? "No input received" : "Hello, $name!";
?>

CLI Color Formatting

Wrapping text in ANSI escape codes tells a compatible terminal to render it in color or with bold/underline styling, which makes warnings and errors visually jump out from ordinary log lines — genuinely useful once a script's output runs to more than a few scrollback screens.

Example: CLI Color Formatting

php
<?php
// Print "\033[31mThis is red text\033[0m\n" to the output
echo "\033[31mThis is red text\033[0m\n";
// Print "\033[1mThis is bold text\033[0m" to the output
echo "\033[1mThis is bold text\033[0m";
?>

Command Line Menu Loop

Looping around a routine that prints a menu, reads the operator's choice, and dispatches to the matching action turns a single-purpose script into a small interactive tool someone can drive by picking numbered options, without needing to remember command-line flags for every feature.

Example: Command Line Menu Loop

php
<?php
$options = ["1" => "View report", "2" => "Exit"];
$choice = "1"; // simulated selection
foreach ($options as $key => $label) {
    echo "$key. $label\n";
}
echo "You chose: " . $options[$choice];
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming $argv behaves like $_GET, when CLI arguments are in the $argv array with the script name first.
  2. Using $_SERVER[REQUEST_METHOD] in a CLI script, where it does not exist.
  3. Forgetting that a CLI script has no web server, so browser-related variables are not set.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.