PHP CLI commands cheat sheet
Version, extensions, and active settings
-
php -vShows the version of PHP that the php command runs. This version can differ from the one your site runs on, as explained further down the page.
-
php -mLists the extensions (modules) loaded on the command line.
-
php -m | grep -i intlChecks whether a specific extension is loaded, here intl. No output means the extension isn't installed or isn't enabled on the command line.
-
php -iShows, as plain text, all the information the phpinfo() function displays: the version, the extensions, and every command line setting.
-
php -i | grep -i memory_limitFilters a single setting out of the output. You can replace memory_limit with any other setting, for example upload_max_filesize or date.timezone.
-
php --iniShows which php.ini file is loaded (Loaded Configuration File) and which additional ini files are scanned from the configuration directory. It's the quickest way to find out which file to edit.
-
php --ini=diffShows only the settings whose value differs from the default. This option exists only in recent PHP versions.
-
php --ri curlShows the settings and information of a single extension, here curl. When the extension isn't loaded, a message says it doesn't exist.
-
php --re mbstringShows the functions, constants, and classes an extension provides, as well as its ini settings.
-
php -r 'echo ini_get("memory_limit"), PHP_EOL;'Runs PHP code directly from the command line, without the <?php tag. Here it prints the active value of memory_limit. Wrap the code in single quotes, so the shell doesn't interpret the $ sign.
-
php -l index.phpChecks a file's syntax without running it. When everything is fine, the message No syntax errors detected appears. The check doesn't catch errors that occur only at runtime, such as a call to a function that doesn't exist. Useful before you upload a changed file to a live site.
-
php -aOpens an interactive shell, where you type PHP code and see the result right away.
-
php -hLists all the command line options, with a short description of each one.
Running with temporary settings
-
php -d memory_limit=-1 script.phpRuns the script with no memory limit, for this run only and without changing php.ini. Caution: a script that keeps consuming memory can use up all of the server's memory, so a high, defined value, for example 1G, is a better choice.
-
php -d display_errors=1 script.phpShows errors in the current run. You can repeat -d several times to change several settings at once.
-
php -c /path/to/custom.ini script.phpRuns the script with a different configuration file, or with a different directory where PHP looks for php.ini. The path is an example.
-
php -n script.phpRuns without any php.ini file, using the default values. Useful to check whether a problem is caused by a setting in the file. Note: extensions that are enabled in the configuration files aren't loaded.
Built-in development server
-
php -S localhost:8000 -t publicStarts PHP's built-in web server at http://localhost:8000, with the public directory as the document root. Caution: the built-in server is meant for development and testing only, it runs as a single process, and you should never use it for a live site or on a public network.
-
php -S localhost:8000 router.phpStarts the server with a router script. When the script returns false, the requested file, such as an image, is served as is.
-
PHP_CLI_SERVER_WORKERS=4 php -S localhost:8000Starts several worker processes, so you can test code that handles several requests at the same time. This option isn't supported on Windows.
CLI vs PHP-FPM
-
php -r 'echo PHP_SAPI, PHP_EOL;'Shows which interface (SAPI) the code runs under. On the command line, the result is cli, and on a site that runs through PHP-FPM, it's fpm-fcgi.
-
php --ini | grep "Loaded Configuration File"Shows the path of the php.ini file loaded on the command line.
-
php-fpm -i | grep "Loaded Configuration File"The same check for PHP-FPM. On Debian and Ubuntu, the command name includes the version, for example php-fpm8.3. When the paths differ, a change in one file doesn't affect the other.
-
sudo php-fpm -tTests the PHP-FPM configuration files and exits, without a restart. Run it before every reload of the service, so a typo doesn't take your sites down.
-
sudo php-fpm -ttTests the configuration and, before exiting, displays all the loaded settings.
-
sudo systemctl reload php-fpmReloads PHP-FPM gracefully: the configuration files are read again and the worker processes are replaced, without fully stopping the service. The service name varies between distributions, for example php-fpm or php8.3-fpm.
-
php_admin_value[memory_limit] = 256MA line in a PHP-FPM pool file, for example www.conf, that sets a value for the sites of that pool only. A value set with php_admin_value can't be changed with ini_set() in the code, and it doesn't affect the command line.
Several PHP versions on one server
-
which -a phpLists all the php executables in the search path (PATH), in order. The first one listed is the one the php command runs.
-
ls /usr/bin/php*Lists the PHP executables installed in the /usr/bin directory. On Debian and Ubuntu, each version has its own file, for example php8.3.
-
php8.3 -vRuns a specific version directly by its file name. The name php8.3 is an example from Debian and Ubuntu; on other distributions and in control panels, the names and paths differ.
-
/usr/bin/php8.3 script.phpRuns a script with a specific version by its full path. This is also how you write a cron entry when the default version differs from the one your site needs. The path is an example.
-
sudo update-alternatives --config phpOn Debian and Ubuntu: lists the installed versions and lets you choose which one the php command runs. The choice affects only the command line, not your site.
-
update-alternatives --display phpShows the selected version, the mode (auto or manual), and all the available alternatives.
-
sudo update-alternatives --set php /usr/bin/php8.3Selects a version without a menu, so it suits scripts. The path is an example.
-
dnf module list phpOn some RHEL-based distributions, you select the PHP version as a module stream, and this command lists the available versions. Recent releases of these distributions have no modules, and PHP is installed directly from the AppStream repository.
-
sudo dnf module enable php:8.3Selects the stream of the version you want before installing, on distributions that support modules. When a different stream is already enabled, run sudo dnf module reset php first. The version in the command is an example.
Installing and enabling extensions
-
php -r 'var_dump(extension_loaded("mbstring"));'Returns true when the extension is loaded, and false when it isn't.
-
sudo apt install php-mbstringOn Debian and Ubuntu: installs an extension from a distribution package. Package names are php followed by the extension name, or for a specific version, for example php8.3-mbstring. After installing, reload PHP-FPM.
-
sudo dnf install php-mbstringOn RHEL-based distributions: installs an extension from a distribution package. After installing, reload PHP-FPM.
-
sudo phpenmod mbstringOn Debian and Ubuntu: enables an extension that's already installed. Without further options, it's enabled for every version and every interface (SAPI).
-
sudo phpdismod -v 8.3 -s cli xdebugDisables an extension for a specific version and interface only, here Xdebug on the command line. The -v option selects a version, and the -s option selects an interface, for example cli or fpm. The version in the command is an example.
-
pecl install redisDownloads, compiles, and installs an extension from PECL. Then enable it in php.ini with the line extension=redis, and reload the service. Note: PECL is deprecated, and PHP recommends PIE instead.
-
php pie.phar install <vendor/package>PIE (PHP Installer for Extensions) is the official tool that replaces PECL. It downloads, compiles, and installs an extension by its package name. Replace <vendor/package> with the extension's package name.
Running scripts and cron
-
#!/usr/bin/phpThe first line of a script (shebang), which lets you run it like any other command. The path must match where php is located on the server. A common alternative is #!/usr/bin/env php, which finds php through PATH.
-
chmod +x script.php && ./script.phpMakes the script executable and runs it directly, provided its first line is a shebang.
-
php script.php arg1 arg2Passes arguments to the script. In the code, they're available in the $argv array, where $argv[0] is the script name, and the $argc variable holds the number of elements in the array.
-
php -r 'var_dump($argv);' -- -hWhen the first argument starts with a hyphen, put -- before it, so PHP passes it to the code instead of treating it as one of its own options.
-
exit(1);Ends the script with an exit code. 0 means success, and any other value means an error. Valid values are 0 to 254, and 255 is reserved by PHP. Shell scripts and monitoring systems rely on this code.
-
echo $?Shows, in the shell, the exit code of the last command that ran.
-
fwrite(STDERR, "Error\n");Writes a message to the error output (STDERR), separately from the regular output (STDOUT). That way, messages don't get mixed into data passed to another command through a pipe.
-
$line = trim(fgets(STDIN));Reads one line from the input (STDIN), for example when you run cat list.txt | php script.php.
-
command -v phpShows the full path of php, which you write in a cron entry.
-
cd /home/user/app && /usr/bin/php task.phpPHP on the command line doesn't change to the script's directory, so relative paths in the code are resolved from the current directory. Change to the directory before running the script, or use __DIR__ in the code. The paths are examples.
-
*/5 * * * * /usr/bin/php /home/user/app/task.php >> /home/user/logs/task.log 2>&1A cron entry that runs a script every five minutes and saves the output and errors to a log file. In cron, write full paths to both php and the script, because the PATH in the cron environment is minimal. The paths are examples.
Execution time, memory, and OPcache
-
php -i | grep max_execution_timeOn the command line, execution time is unlimited (the value 0), even when php.ini sets a different value, so a long script isn't stopped for running too long, unlike a request on a site.
-
php -r 'echo memory_get_peak_usage(true), PHP_EOL;'Shows the code's peak memory usage in bytes. At the end of a script, the function helps you estimate how much memory it needs. The memory_limit setting applies on the command line too.
-
php -i | grep opcache.enable_cliOPcache is off on the command line by default (opcache.enable_cli=0). For short scripts, it usually doesn't help, because every run is a new process.
-
php -d opcache.enable_cli=1 script.phpEnables OPcache for a single run. The setting can be changed only in php.ini or on the command line, not with ini_set() in the code.
Common errors
-
php: command not foundPHP for the command line isn't installed, or it isn't in PATH. Check with which -a php or ls /usr/bin/php*, then run it by its full path, or install the distribution's php-cli package.
-
Could not open input file: script.phpThe file wasn't found at the given path, usually because the command runs from a different directory. Run it from the right directory or with a full path, and check that the file is readable.
-
Call to undefined function mb_strlen()The function belongs to an extension that isn't loaded, here mbstring. Check with php -m, and install or enable the extension. When the error appears only on the command line or only on the site, the extension is enabled in just one of them.
-
Allowed memory size of 134217728 bytes exhaustedThe script went over the memory_limit setting, here 128M. Raise the setting in the command line php.ini file, or for a single run with php -d memory_limit=512M, and check whether the code loads too much data at once.
-
Class "App\Models\Order" not foundComposer's autoloader can't find the class: a new class, a file name or namespace that doesn't match, or a missing vendor directory. Run composer dump-autoload, and when the vendor directory is missing, run composer install.
-
PHP Parse error: syntax errorA syntax error in the code. Running php -l on the file shows the problematic line. When the code works in one version and fails in another, check the version with php -v.
-
php -v && which phpWhen the command line version differs from your site's: the site runs the version selected for it, while the php command runs the system's default version. Run the right version by its full path.
No matching commands.
PHP on the command line (CLI) and the PHP that runs your site, for example through PHP-FPM, are two separate setups, sometimes with different versions and different configuration files. On Debian and Ubuntu, for example, there's a /etc/php/<version>/cli/php.ini file for the command line and a /etc/php/<version>/fpm/php.ini file for PHP-FPM. On RHEL-based distributions, the /etc/php.ini file is shared, but PHP-FPM pool files can override its values. So a setting you change in one place doesn't necessarily affect the other. The php -i command shows the command line settings, and the phpinfo() function in a temporary test file shows the settings your site sees; delete the test file when you're done. The settings themselves are explained on the php.ini directives page.
In control panels such as DirectAdmin and cPanel, you choose a PHP version for each domain: in DirectAdmin on the Domain Setup screen, and in cPanel with the MultiPHP Manager tool. The php command on the command line may run a different version, so in scripts and cron entries, write the full path of the right version. You can build the schedule with the Cron expression generator, manage the project's packages with Composer commands, and run Laravel projects with Laravel Artisan commands. Support dates for each version are on the PHP lifecycle page.
On a Linux VPS, you connect over SSH with full root access, install several PHP versions side by side, and run every command on this page. With Linux hosting plans, you choose the PHP version for each site in the control panel and set up scheduled tasks (cron jobs) that run PHP scripts.
PHP CLI commands FAQ
What's the difference between php -i and phpinfo() on a site?
The php -i command shows PHP's command line settings, based on the command line php.ini file. The phpinfo() function in a test file on your site shows the settings as the site sees them, including changes from a .user.ini file and from the PHP-FPM pool settings. When a setting change doesn't show up on the site, check the value in both places, and delete the test file when you're done.
Why is the PHP version on the command line different from my site's version?
Your site runs the version selected for it, for example in the control panel, while the php command runs the system's default version. To run a script with your site's version, write the full path of that version. On Debian and Ubuntu, you can change the default with update-alternatives --config php, and the change affects only the command line.
How do I run a PHP script on a schedule with cron?
In the cron entry, write full paths to php and to the script, and redirect the output to a log file, for example: */5 * * * * /usr/bin/php /home/user/app/task.php >> /home/user/logs/task.log 2>&1. You can find the path of php with command -v php, and build the schedule easily with the Cron expression generator. On Linux hosting plans, you set up the task in the control panel, and on a VPS, you can also use crontab -e.
How do I check whether a PHP extension is installed and enabled?
On the command line, run php -m and look for the extension name, or run php --ri with the extension name to see its settings. On the site itself, extensions appear in the phpinfo() output. Install a missing extension from a distribution package, enable it, and then reload PHP-FPM, so your site picks it up too.
How do I run a PHP site on my computer for development and testing?
PHP includes a built-in web server that's made for exactly this. In the project directory, run php -S localhost:8000 -t public, and open http://localhost:8000 in your browser. The built-in server is meant for development and testing only, and a live site runs on a full web server with PHP-FPM, for example on a Linux VPS.