CLI applications
$argv and $argc
Command line scripts receive arguments in $argv, with the count in $argc. The first element is the script name itself.
Read user values from index one onward. Provide a default when the argument is missing so the tool stays friendly.
$name = $argv[1] ?? "stranger";
echo "Hello " . $name . "\n";
Exit codes
Exit codes tell the shell whether the tool succeeded. Zero means success and any other number signals a specific failure.
Print usage help before exiting with an error. Other scripts can then react to the code in automation.
if ($argc < 2) {
echo "Usage\n";
exit(1);
}
echo "ok";
exit(0);
ANSI colors
Terminals understand escape sequences that change text color. Wrap important words in a color and reset the style right after.
Keep colors optional for logs and pipes. Plain output remains easier to search and store.
$green = "\033[32m";
$reset = "\033[0m";
echo $green . "ok" . $reset . "\n";
Readline sketch
Function readline asks a question and waits for one line of typed input. It is ideal for setup wizards and confirmations.
Validate the answer before continuing. Ask again when the input is empty or has the wrong shape.
$name = readline("Your name: ");
echo "Hello " . $name . "\n";
Next: SPL essentials
Article author: Arthur Isaev