PHP Command Line (CLI)
In this page:
// 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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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!";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
$options = ["1" => "View report", "2" => "Exit"];
$choice = "1"; // simulated selection
foreach ($options as $key => $label) {
echo "$key. $label\n";
}
echo "You chose: " . $options[$choice];
?>
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
$argv$_GETजैसा behave करता है, जबकि CLI arguments$argvarray में हैं script name पहले के साथ। - किसी CLI script में
$_SERVER[REQUEST_METHOD]इस्तेमाल करना, जहाँ यह exist नहीं करता। - यह भूल जाना कि एक CLI script का कोई web server नहीं है, इसलिए browser-related variables set नहीं होते।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: