Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Concepts and Reference

The PHP command line

Reference for running PHP from the command line: php options, arguments, STDIN, STDOUT, STDERR, exit codes, the built-in server and CLI-only constants.

The PHP command-line interface (CLI) runs scripts from a terminal instead of a web server. It is the right tool for cron jobs, queue workers, build scripts, migrations and one-off tools. The same language runs in both worlds, but the CLI has no request, no $_GET, and no HTML headers. Instead it has arguments, standard streams and an exit code.

Text
php script.php one two        run a file with arguments
php -r 'echo PHP_VERSION;'    run code from the command line
php -S localhost:8000         start the built-in development web server
php -v                        print the version
php -l script.php             syntax check only
php -i                        print configuration (phpinfo)
php -m                        list loaded extensions

Try it

PHP What the CLI gives you: SAPI, streams and exit codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Parsing command-line style arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Command-line options

Option Meaning
-r 'code' Run code without <?php tags
-f file Run a file (the plain php file form is the same)
-l file Lint: check syntax without running
-a Interactive shell, when built with readline
-S host:port Built-in web server, for development only
-t dir Document root for -S
-c path Use this php.ini
-n Ignore every php.ini
-d name=value Set an ini setting for this run
-m, -i, -v Modules, info, version
--ri name Information about one extension
-- Everything after it goes to the script, not to PHP

CLI-specific features

  • $argv (list of strings, $argv[0] is the script) and $argc (their count) are available at top level. $_SERVER['argv'] holds the same list.
  • getopt() parses short and long options from the real command line, for example getopt("vf:", ["dry-run", "name:"]). It stops at the first non-option argument.
  • STDIN, STDOUT and STDERR are predefined stream constants. Write errors to STDERR so they stay out of piped output.
  • exit(int) sets the exit status: 0 means success and anything else means failure. A fatal error exits with 255.
  • fgets(STDIN) reads a line, readline() (if available) adds editing and history, and stream_isatty(STDOUT) tells you whether output goes to a terminal.
  • The CLI has no time limit by default (max_execution_time is 0) and uses display_errors according to your ini.
  • php_sapi_name() and PHP_SAPI return cli, which is how libraries decide to print plain text instead of HTML.

Usage notes

  • The built-in server (-S) is single-process and for local development only. Never expose it to the internet.
  • Cron jobs may use a different PHP binary or php.ini than your web server. Print php --ini to see which one is active.
  • Shell quoting differs: use single quotes with -r on Linux and macOS, double quotes on Windows cmd.
  • Long-running CLI scripts should watch memory (see memory and garbage collection) and handle signals with pcntl where available.
  • This site runs examples as CLI scripts, so web features (headers, cookies, sessions) are simulated or shown read-only.