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.
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
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.
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 examplegetopt("vf:", ["dry-run", "name:"]). It stops at the first non-option argument.STDIN,STDOUTandSTDERRare predefined stream constants. Write errors toSTDERRso they stay out of piped output.exit(int)sets the exit status:0means success and anything else means failure. A fatal error exits with255.fgets(STDIN)reads a line,readline()(if available) adds editing and history, andstream_isatty(STDOUT)tells you whether output goes to a terminal.- The CLI has no time limit by default (
max_execution_timeis 0) and usesdisplay_errorsaccording to your ini. php_sapi_name()andPHP_SAPIreturncli, 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.inithan your web server. Printphp --inito see which one is active. - Shell quoting differs: use single quotes with
-ron Linux and macOS, double quotes on Windowscmd. - Long-running CLI scripts should watch memory (see memory and garbage collection) and handle signals with
pcntlwhere available. - This site runs examples as CLI scripts, so web features (headers, cookies, sessions) are simulated or shown read-only.