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

PHP Command Line (CLI)

PHP command line आपको एक web page के बजाय सीधे terminal window से PHP scripts चलाने देता है। यह cleanup tasks और quick tools जैसे chores के लिए handy है।
Syntax
php
// php script.php argument1 argument2
$script_name = $argv[0];
$argument = $argv[1];

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

PHP CLI क्या है?

PHP CLI (Command Line Interface) एक .php script को इसे किसी web server request के through route करने के बजाय सीधे एक terminal से चलाता है — background jobs, scheduled cron tasks, और one-off maintenance scripts के लिए सही tool जिनमें कोई browser शामिल नहीं और HTML produce करने की बिल्कुल ज़रूरत नहीं।

उदाहरण: 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

command line पर script की filename के बाद आप जो भी argument type करते हैं वह built-in $argv array में पहुँचता है, $argv[0] हमेशा पहले script की खुद की filename रखते हुए। $argc आपको total count देता है, जो script आगे बढ़ने से पहले यह validate करने के लिए handy है कि expected संख्या में arguments actually supply किए गए।

उदाहरण: 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

fgets(STDIN) call करना script execution pause कर देता है और इसे चलाने वाले व्यक्ति के एक line type करके Enter दबाने का wait करता है, जो एक script को fire-and-forget tool से बदलकर mid-run interactively confirmation माँगने या एक value collect करने वाली चीज़ बना देता है।

उदाहरण: 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

text को ANSI escape codes में wrap करना एक compatible terminal को इसे color में या bold/underline styling के साथ render करने के लिए बताता है, जो warnings और errors को ordinary log lines से visually अलग दिखाता है — genuinely उपयोगी एक बार किसी script का output कुछ scrollback screens से ज़्यादा चला जाए।

उदाहरण: 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

एक routine के आसपास loop चलाना जो एक menu print करती है, operator की choice पढ़ती है, और matching action पर dispatch करती है एक single-purpose script को एक छोटे interactive tool में बदल देता है जिसे कोई numbered options चुनकर चला सके, हर feature के लिए command-line flags याद रखने की ज़रूरत के बिना।

उदाहरण: 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. #}
आम गलतियां
  1. यह मान लेना कि $argv $_GET जैसा behave करता है, जबकि CLI arguments $argv array में हैं script name पहले के साथ।
  2. किसी CLI script में $_SERVER[REQUEST_METHOD] इस्तेमाल करना, जहाँ यह exist नहीं करता।
  3. यह भूल जाना कि एक CLI script का कोई web server नहीं है, इसलिए browser-related variables 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.