LinuxCommandLibrary

php

Execute PHP scripts from the command line

TLDR

Parse and execute a PHP script

$ php [path/to/file]
copy

Check syntax on (i.e. [l]int) a PHP script
$ php [[-l|--syntax-check]] [path/to/file]
copy

Run PHP inter[a]ctively
$ php [[-a|--interactive]]
copy

Run PHP code (Notes: Don't use tags; escape double quotes with backslash)
$ php [[-r|--run]] "[code]"
copy

Start a PHP built-in web [S]erver in the current directory
$ php [[-S|--server]] [host]:[port]
copy

List installed PHP extensions
$ php [[-m|--modules]]
copy

Display information about the current PHP configuration
$ php [[-i|--info]]
copy

Display information about a specific function
$ php [[--rf|--rfunction]] [function_name]
copy

SYNOPSIS

php [options] [-f <file> | --file <file>] [--] [arguments...]
php [options] -r <code> [--] [arguments...]
php [options] -a
php [options] -S <addr:port> [-t <docroot>]
php [options] -l <file>

PARAMETERS

-a, --interactive
    Run PHP in interactive shell mode (REPL), allowing direct execution of PHP code.

-c <path>, --php-ini <path>
    Specify the path to a php.ini file or directory for configuration.

-C, --no-php-ini
    Do not load the default php.ini file. Useful for clean execution environments.

-d foo[=bar], --define foo[=bar]
    Define an INI entry foo with an optional value bar. Overrides php.ini settings.

-f <file>, --file <file>
    Parse and execute the specified PHP <file>. This is the default action if no other mode is specified.

-h, --help
    Display a brief help message with available options.

-i, --info
    Output the PHP information page, similar to phpinfo() in a web browser.

-l, --syntax-check
    Perform a syntax check on the specified PHP file without executing it. Returns 0 for success, non-zero for errors.

-m, --modules
    Show all compiled and loaded PHP modules and extensions.

-n, --no-ini
    No php.ini file will be used. Similar to -C, but implies other default configurations.

-r <code>, --run <code>
    Run PHP <code> directly from the command line, without needing <?php ... ?> tags.

-S <addr:port>, --server <addr:port>
    Start the built-in PHP development web server. Specify the address and port (e.g., localhost:8000).

-t <docroot>, --doc-root <docroot>
    Specify the document root for the built-in web server. Used with -S.

-v, --version
    Display the PHP version information.

-z <file>, --zend-extension <file>
    Load a Zend extension (e.g., Xdebug). Specify the full path to the extension .so or .dll file.

--
    Marks the end of options and treats all subsequent arguments as script arguments, even if they start with a hyphen.

DESCRIPTION

The php command serves as the command-line interface (CLI) for the PHP scripting language. It enables users to execute PHP scripts directly from the terminal, making it ideal for automation tasks, cron jobs, server-side scripting, and running CLI applications built with PHP. Beyond simple script execution, the php CLI also provides a powerful interactive shell (REPL), a built-in development web server, and utilities for checking PHP configuration, loaded modules, and syntax.

It's a versatile tool crucial for PHP developers, allowing them to test code snippets, debug applications, and manage projects without relying on a traditional web server setup. While primarily used for CLI operations, its functionality extends to being the core interpreter for web servers like Apache and Nginx (often via PHP-FPM) to process dynamic web content.

CAVEATS

The built-in web server (started with -S) is designed for development purposes only and should not be used in production environments due to performance and security limitations. When running short-lived CLI scripts, PHP incurs a startup overhead, which might be noticeable for very frequent executions. Ensure proper handling of arguments passed to scripts, especially when using --, to prevent unexpected behavior. Security best practices, such as sanitizing input and restricting file system access, are crucial when deploying PHP applications.

CONFIGURATION FILES

The php command respects the PHP configuration hierarchy. It first looks for a php.ini file in its default locations (e.g., /etc/php/<version>/cli/php.ini on Debian/Ubuntu, or system-wide configurations). The -c option allows specifying a custom php.ini, and -d allows overriding individual INI directives directly from the command line. The -n or -C options can prevent any php.ini from being loaded, providing a clean execution environment.

EXIT STATUS

The php command returns an exit status of 0 on success. A non-zero exit status indicates an error. For example, 255 is often used for general script execution failures or uncaught exceptions, while a non-zero exit status can also be explicitly set within a PHP script using exit() or die().

HISTORY

PHP (originally 'Personal Home Page Tools') was created by Rasmus Lerdorf in 1994 as a set of CGI binaries written in C. It was initially used for tracking visits to his online resume. Over time, it evolved into a more powerful scripting language. The significant shift came with PHP 3 (1997), which introduced the Zend Engine and expanded its capabilities beyond simple web forms. PHP 4 (2000) brought further performance improvements with Zend Engine 1.0. PHP 5 (2004) introduced a robust object model (Zend Engine 2.0). The most impactful recent release was PHP 7 (2015), which delivered massive performance gains and new language features. PHP 8 (2020) continued this trend with a JIT compiler and further enhancements, solidifying PHP's position as a robust language for web development and command-line scripting.

SEE ALSO

php-fpm(8), apache2(8), nginx(8), composer(1)

Copied to clipboard