PHP is most commonly used for web development, but it also has a powerful Command Line Interface (CLI). This allows developers to run PHP scripts directly in the terminal, automate tasks, interact with the shell, and even run PHP without a web server.
Running PHP Scripts from the Command Line
The basic way to execute a PHP script is:
php script.php
If the script does not have the .php extension, you can still run it as long as it contains valid PHP code.
You can also run inline code:
php -r "echo 1;"
Here, -r runs the code without needing <?php ... ?> tags.
Command-Line Options
The list of command line options provided by the PHP binary can be queried at any time by running PHP with the -h switch:
>php -h
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
php [options] -S <addr>:<port> [-t docroot] [router]
php [options] -- [args...]
php [options] -a
-a Run as interactive shell (requires readline extension)
-c <path>|<file> Look for php.ini file in this directory
-n No configuration (ini) files will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse and execute <file>.
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-r <code> Run PHP <code> without using script tags <?..?>
-B <begin_code> Run PHP <begin_code> before processing input lines
-R <code> Run PHP <code> for every input line
-F <file> Parse and execute <file> for every input line
-E <end_code> Run PHP <end_code> after processing all input lines
-H Hide any passed arguments from external tools.
-S <addr>:<port> Run with built-in web server.
-t <docroot> Specify document root <docroot> for built-in web server.
-s Output HTML syntax highlighted source.
-v Version number
-w Output source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin
--ini Show configuration file names
--rf <name> Show information about function <name>.
--rc <name> Show information about class <name>.
--re <name> Show information about extension <name>.
--rz <name> Show information about Zend extension <name>.
--ri <name> Show configuration for extension <name>.
Input/Output Streams
PHP CLI works with standard I/O streams:
Usage example:
echo "Enter your name: "; $name = trim(fgets(STDIN)); fwrite(STDOUT, "Hello, $name!\n"); fwrite(STDERR, "This is an error message\n");
Run it:
php input.php Enter your name: Orkhan Hello, Orkhan! This is an error message
Interactive Shell (REPL)
PHP CLI provides an interactive shell mode:
php -a
Allows entering PHP code line by line, similar to Python's REPL (Read - Eval - Print Loop).
php -a
Interactive shell
php > echo 2+3;
5
php > echo strtoupper("cli");
"CLI"
Built-in Web Server
Since PHP 5.4, CLI includes a lightweight web server for development:
php -S localhost:8000
php -S localhost:8000 router.php
router.php can control how requests are handled (useful for frameworks).
php.ini Settings for CLI
PHP CLI uses its own php.ini file (separate from Apache/Nginx).
Check with:
php --ini
You might see:
Configuration File (php.ini) Path: /etc/php/8.2/cli Loaded Configuration File: /etc/php/8.2/cli/php.ini
Common CLI-related settings:
CLI usually ignores timeouts (max_execution_time) since it's meant for long-running scripts.
Execute PHP Script as Shell Script
You can run a PHP file like a shell script by using a shebang and making the file executable.
Put a shebang as the very first line:
#!/usr/bin/env php <?php // your PHP code...
Make the file executable:
chmod +x myscript.php
Run it:
./myscript.php arg1 arg2
Source: Orkhan Alishov's notes