php — The PHP Command Line

Practical guide to the PHP CLI: run scripts, start the built-in web server, lint code, inspect extensions and explore the PHP environment.

The php binary is not just the interpreter behind your web server – it is a full-featured command-line tool for everyday work. You use it to run scripts, spin up a built-in web server for local development with -S, open an interactive REPL with -a, and check a file's syntax without running it using -l. With -i and -r you inspect configuration and environment or execute PHP code straight from the shell – ideal for quick one-liners, debugging and automation. This guide walks you through the most useful flags, from running scripts and linting to Xdebug.

Running Scripts

php <file> — Execute a PHP script.

php script.php

php -f <file> — Explicitly specify the file to execute (same as without -f).

php -f migrate.php

php -r '<code>' — Run PHP code directly from the command line without a file.

php -r 'echo PHP_VERSION . PHP_EOL;'

php -r '<code>' -- <arg1> <arg2> — Run inline code with arguments. Access via $argv.

php -r 'var_dump($argv);' -- hello world

echo '<input>' | php <file> — Pipe data into a PHP script via stdin.

echo '{"name":"John"}' | php -r 'echo json_decode(file_get_contents("php://stdin"))->name;'

php -a — Start the interactive PHP shell (REPL).

php -a

php -B '<before>' -R '<each_line>' <file> — Process each line of input. -B runs once before, -R runs for each line ($argn = current line).

cat data.txt | php -B 'echo "Start\n";' -R 'echo strtoupper($argn) . PHP_EOL;'

Built-in Web Server

php -S localhost:<port> — Start the built-in development web server.

php -S localhost:8000

php -S localhost:<port> -t <docroot> — Start the server with a specific document root directory.

php -S localhost:8000 -t public/

php -S 0.0.0.0:<port> — Start the server accessible from other devices on the network.

php -S 0.0.0.0:8000

php -S localhost:<port> <router> — Start the server with a custom router script.

php -S localhost:8000 router.php

PHP_CLI_SERVER_WORKERS=<n> php -S localhost:<port> — Start the server with multiple workers for parallel request handling (PHP 7.4+).

PHP_CLI_SERVER_WORKERS=4 php -S localhost:8000 -t public/

Syntax Checking & Linting

php -l <file> — Check a file for syntax errors without executing it.

php -l index.php

find <dir> -name '*.php' -exec php -l {} \; — Lint all PHP files in a directory recursively.

find ./src/ -name '*.php' -exec php -l {} \;

find <dir> -name '*.php' -print0 | xargs -0 -n1 php -l — Lint all PHP files safely, handling filenames with spaces.

find ./src/ -name '*.php' -print0 | xargs -0 -n1 php -l

php -l <file> 2>&1 | grep -v 'No syntax errors' — Lint a file and show output only if there are errors.

find . -name '*.php' -exec php -l {} \; 2>&1 | grep -v 'No syntax errors'

Version & Environment

php -v — Show the PHP version, Zend Engine version, and loaded modules.

php -v

php -i — Show full phpinfo() output on the command line.

php -i

php -i | grep <setting> — Search for a specific setting in the PHP configuration.

php -i | grep memory_limit

php -r 'phpinfo();' | grep '<directive>' — Alternative way to search PHP configuration values.

php -r 'phpinfo();' | grep 'upload_max_filesize'

php -r 'echo php_ini_loaded_file() . PHP_EOL;' — Show the path to the loaded php.ini file.

php -r 'echo php_ini_loaded_file() . PHP_EOL;'

php --ini — Show all loaded configuration files (php.ini and additional .ini files).

php --ini

php -r 'echo PHP_INT_SIZE * 8 . "-bit" . PHP_EOL;' — Check if PHP is running in 32-bit or 64-bit mode.

php -r 'echo PHP_INT_SIZE * 8 . "-bit" . PHP_EOL;'

Extensions & Modules

php -m — List all loaded PHP extensions.

php -m

php -m | grep <extension> — Check if a specific extension is loaded.

php -m | grep mbstring

php -r 'echo extension_loaded("<ext>") ? "yes" : "no";' — Programmatically check if an extension is available.

php -r 'echo extension_loaded("redis") ? "yes" : "no";'

php --re <extension> — Show detailed information about an extension (classes, functions, constants).

php --re json

php --ri <extension> — Show configuration information for an extension (like phpinfo section).

php --ri opcache

php -d extension=<name> <file> — Load an extension for this execution only.

php -d extension=xdebug script.php

Configuration Overrides

php -d <directive>=<value> <file> — Override a php.ini directive for this execution.

php -d memory_limit=512M import.php

php -d memory_limit=-1 <file> — Remove the memory limit (unlimited). Useful for large data processing.

php -d memory_limit=-1 migrate.php

php -d display_errors=On -d error_reporting=E_ALL <file> — Enable all error reporting for debugging.

php -d display_errors=On -d error_reporting=E_ALL buggy-script.php

php -d max_execution_time=0 <file> — Remove the execution time limit for long-running scripts.

php -d max_execution_time=0 long-import.php

php -d opcache.enable_cli=1 <file> — Enable OPcache for CLI scripts (disabled by default).

php -d opcache.enable_cli=1 benchmark.php

php -n <file> — Run without any php.ini file. Useful for testing with vanilla PHP settings.

php -n -r 'echo ini_get("memory_limit");'

php -c <ini_path> <file> — Use a specific php.ini file or directory.

php -c /etc/php/cli/php.ini script.php

Code Analysis & Debugging

php -w <file> — Strip all comments and whitespace from a PHP file.

php -w verbose-script.php

php --rf <function> — Show the signature and details of a built-in function (reflection).

php --rf array_map

php --rc <class> — Show the signature, methods, and properties of a built-in class.

php --rc DateTime

php -r 'print_r(get_defined_functions()["internal"]);' — List all available built-in functions.

php -r 'print_r(get_defined_functions()["internal"]);'

php -r 'var_dump(get_loaded_extensions());' — Show all loaded extensions as an indexed array.

php -r 'var_dump(get_loaded_extensions());'

One-Liners & Data Processing

php -r 'echo json_encode(<data>, JSON_PRETTY_PRINT);' — Pretty-print JSON data.

php -r 'echo json_encode(["name" => "John", "age" => 30], JSON_PRETTY_PRINT);'

php -r 'echo base64_encode("<text>");' — Base64 encode a string.

php -r 'echo base64_encode("Hello World") . PHP_EOL;'

php -r 'echo base64_decode("<encoded>");' — Base64 decode a string.

php -r 'echo base64_decode("SGVsbG8gV29ybGQ=") . PHP_EOL;'

php -r 'echo urlencode("<text>");' — URL encode a string.

php -r 'echo urlencode("hello world & foo=bar") . PHP_EOL;'

php -r 'echo password_hash("<password>", PASSWORD_DEFAULT);' — Generate a secure password hash.

php -r 'echo password_hash("MySecretPass", PASSWORD_DEFAULT) . PHP_EOL;'

php -r 'echo bin2hex(random_bytes(<n>));' — Generate a cryptographically secure random hex string.

php -r 'echo bin2hex(random_bytes(32)) . PHP_EOL;'

php -r 'echo hash("sha256", "<text>");' — Generate a hash of a string.

php -r 'echo hash("sha256", "Hello World") . PHP_EOL;'

php -r 'echo date("Y-m-d H:i:s", <timestamp>);' — Convert a Unix timestamp to a human-readable date.

php -r 'echo date("Y-m-d H:i:s", 1700000000) . PHP_EOL;'

php -r 'echo strtotime("<date>");' — Convert a date string to a Unix timestamp.

php -r 'echo strtotime("2024-06-15 12:00:00") . PHP_EOL;'

php -r 'echo serialize(<data>);' — Serialize a PHP value for storage.

php -r 'echo serialize(["a" => 1, "b" => 2]) . PHP_EOL;'

Xdebug & Profiling

php -d xdebug.mode=debug -d xdebug.start_with_request=yes <file> — Run a script with Xdebug step debugging enabled.

php -d xdebug.mode=debug -d xdebug.start_with_request=yes script.php

php -d xdebug.mode=profile <file> — Profile a script. Generates a cachegrind file for analysis.

php -d xdebug.mode=profile -d xdebug.output_dir=/tmp slow-script.php

php -d xdebug.mode=trace <file> — Trace all function calls. Generates a trace file.

php -d xdebug.mode=trace -d xdebug.output_dir=/tmp script.php

XDEBUG_MODE=off php <file> — Disable Xdebug entirely for better performance (e.g. Composer).

XDEBUG_MODE=off php composer.phar install

Package & Archive

php -r 'echo Phar::running();' — Check if the current script is running from inside a PHAR archive.

php -r 'echo Phar::running() ?: "Not in a PHAR" . PHP_EOL;'

php <phar_file> — Execute a PHAR (PHP Archive) file directly.

php composer.phar install

php -d phar.readonly=0 <file> — Allow creating or modifying PHAR archives (disabled by default for security).

php -d phar.readonly=0 build-phar.php

Conclusion

The PHP command line is far more than a script runner: lint with -l, experiment in the REPL with -a, inspect the environment with -i and --re, and override individual ini directives on the fly with -d – that covers nearly every development and debugging need. Two things are worth remembering: the built-in web server php -S is meant strictly for local development and must never face the public internet, as it offers no hardening or load resilience. And -r as well as -B/-R execute arbitrary PHP code – so never pass such one-liners around from untrusted sources, and check what you run.

Further Reading

  • artisan – command-line tool of the Laravel framework
  • cargo – package manager and build tool for Rust
  • composer – dependency manager for PHP projects