CLI applications

The same PHP binary that answers a web request also runs a command line tool, and the difference is where the input comes from. The examples below are run with the commands shown, so each argument, status code and prompt on the page is what the terminal printed.

$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.

<?php $name = $argv[1] ?? "stranger"; echo "Hello " . $name . "\n"; echo $argc, "\n";

php hello.php Ada

Hello Ada
2

The count is two because the script name and the argument both sit in the list; the default would answer Hello stranger for a run without arguments.

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.

<?php if ($argc < 2) { echo "Usage: php status.php NAME\n"; exit(1); } echo "ok\n"; exit(0);

php status.php

Usage: php status.php NAME

The run ends with status 1, which echo $? shows in the shell and which a script calling this tool receives. A run with a name prints ok and leaves status 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.

<?php $green = "\033[32m"; $reset = "\033[0m"; $line = $green . "ok" . $reset; echo bin2hex($green), PHP_EOL; echo str_replace("\033", "<ESC>", $line), PHP_EOL;

php colors.php

1b5b33326d
<ESC>[32mok<ESC>[0m

The hex line is the five bytes of the green sequence, and the second line shows where the invisible bytes sit around the word. A terminal turns them into color, and a log file keeps them as noise, which is why the plain form is the safe default.

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.

<?php $name = readline("Your name: "); if ($name === false) { $name = "stranger"; } echo "Hello " . $name . "\n";

php ask.php

Hello stranger

The run had no terminal to read from, so readline() returned false and the fallback name was used. A real terminal sends the typed line instead, and the prompt appears before it.

Exercises

  1. Run the hello example without arguments and read the default it prints.
  2. Add a second argument and print both values in one line.
  3. Change the usage exit to status 2 and read it with echo $?.
  4. Make the color conditional on stream_isatty(STDOUT) and describe both runs.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is $argv[0] in a PHP command line script?

    Answer

    The name of the script

    The script name takes the first slot, which is why the user values start at index one.

  2. What does an exit status of zero mean?

    Answer

    The tool finished successfully

    A non-zero status is the failure signal, and the exact number tells which failure.

  3. Why does a colored line end with "\033[0m"?

    Answer

    It resets the style so later text is plain again

    Without the reset every following word keeps the last color, including the shell prompt.

  4. What does readline() return when the input stream ends?

    Answer

    false

    The function reports the end of input with false, which is why the example checks for it.

  5. What does this program print? <?php $argv = ["hello.php", "Ada"]; $argc = count($argv); $name = $argv[1] ?? "stranger"; echo "Hello " . $name, "\n"; echo $argc;

    Answer

    Hello Ada / 2

    The first argument is Ada, and the count includes the script name.

  6. What does this program print? <?php $argv = ["count.php"]; $argc = count($argv); echo $argc, "\n"; echo isset($argv[1]) ? "value" : "missing";

    Answer

    1 / missing

    A run with no arguments has one entry, the script name, so index one is not set.

  7. What does this program print? <?php $green = "\033[32m"; echo bin2hex($green);

    Answer

    1b5b33326d

    The escape byte is 1b, and the rest of the sequence follows in hex.

  8. What does this program print? <?php if ($argc < 2) { echo "Usage\n"; exit(3); } echo "ok\n";

    Answer

    Usage, then status 3

    The branch prints the usage line and leaves with the status the call names.

  9. Why must a tool check $argv[1] before using it?

    Answer

    The index does not exist when the argument is missing, and PHP warns

    A missing argument gives a warning and null, so the ?? default or an explicit check keeps the tool calm.

  10. Why are colors a bad default for a tool whose output is piped into a log?

    Answer

    The escape bytes stay in the text and make searching harder

    A file keeps the invisible bytes, so a log line no longer matches a plain search for its text.

The examples call readline(), bin2hex() and exit(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above.

Article author: Arthur Isaev