php(1)
PHP command-line interface for executing PHP scripts and running an interactive shell.
Synopsis
php [options] [-f] file [--args arguments]Description
PHP is a server-side scripting language commonly used for web development, but the command-line interface allows you to execute PHP scripts directly from the terminal without a web server. You can run standalone scripts, process files, or interact with PHP in an interactive shell mode.
The CLI version includes all core PHP functionality and can access databases, read/write files, and execute system commands. It's useful for batch processing, cron jobs, command-line tools, and testing PHP code.
Common options
| Flag | What it does |
|---|---|
-f file | Parse and execute the named file (default behavior, often omitted) |
-r code | Execute the given PHP code without opening a file; useful for one-liners |
-i | Display PHP information (phpinfo), equivalent to phpinfo() function |
-v | Show PHP version and build information |
-m | Show loaded extensions and modules |
-l file | Syntax check only; do not execute (lint mode) |
-d option=value | Set an ini directive; can be used multiple times |
-S addr:port | Start a local development server on given address and port |
-a | Enter interactive PHP shell (REPL mode) |
-t docroot | Specify document root for built-in server |
-w | Output source with all comments and whitespace stripped |
--ini | Show loaded configuration files and directives |
Examples
Execute a PHP script file directly from the command line
php script.phpExecute PHP code directly without creating a file
php -r 'echo "Hello, World!\n";'Start a built-in web server for development on port 8000
php -S localhost:8000Check PHP file syntax without executing it
php -l config.phpExecute script with temporary ini override to show errors
php -d display_errors=1 script.phpStart interactive PHP shell (REPL) for testing code snippets
php -aDisplay PHP info and filter for the active configuration file path
php -i | grep 'Loaded Configuration File'List all loaded modules and check if PDO extension is installed
php -m | grep pdo