# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/build-languages/php/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Running Scripts

`php <file>` — Execute a PHP script.

```bash
php script.php
```

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

```bash
php -f migrate.php
```

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

```bash
php -r 'echo PHP_VERSION . PHP_EOL;'
```

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

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

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

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

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

```bash
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).

```bash
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.

```bash
php -S localhost:8000
```

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

```bash
php -S localhost:8000 -t public/
```

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

```bash
php -S 0.0.0.0:8000
```

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

```bash
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+).

```bash
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.

```bash
php -l index.php
```

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

```bash
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.

```bash
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.

```bash
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.

```bash
php -v
```

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

```bash
php -i
```

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

```bash
php -i | grep memory_limit
```

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

```bash
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.

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

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

```bash
php --ini
```

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

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

## Extensions & Modules

`php -m` — List all loaded PHP extensions.

```bash
php -m
```

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

```bash
php -m | grep mbstring
```

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

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

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

```bash
php --re json
```

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

```bash
php --ri opcache
```

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

```bash
php -d extension=xdebug script.php
```

## Configuration Overrides

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

```bash
php -d memory_limit=512M import.php
```

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

```bash
php -d memory_limit=-1 migrate.php
```

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

```bash
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.

```bash
php -d max_execution_time=0 long-import.php
```

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

```bash
php -d opcache.enable_cli=1 benchmark.php
```

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

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

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

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

## Code Analysis & Debugging

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

```bash
php -w verbose-script.php
```

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

```bash
php --rf array_map
```

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

```bash
php --rc DateTime
```

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

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

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

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

## One-Liners & Data Processing

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

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

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

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

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

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

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

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

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

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

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

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

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

```bash
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.

```bash
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.

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

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

```bash
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.

```bash
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.

```bash
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.

```bash
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).

```bash
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.

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

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

```bash
php composer.phar install
```

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

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

<!-- PROSE:outro -->
## 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

- [PHP Manual: Command line usage](https://www.php.net/manual/en/features.commandline.php) – official documentation for the PHP CLI
- [PHP Manual: Built-in web server](https://www.php.net/manual/en/features.commandline.webserver.php) – details and security notes for `php -S`
<!-- PROSE:outro:end -->

## Related Commands

- [artisan](https://www.jpkc.com/db/en/cheatsheets/build-languages/artisan/) – command-line tool of the Laravel framework
- [cargo](https://www.jpkc.com/db/en/cheatsheets/build-languages/cargo/) – package manager and build tool for Rust
- [composer](https://www.jpkc.com/db/en/cheatsheets/build-languages/composer/) – dependency manager for PHP projects

