
php Command in Linux
The php command in Linux runs PHP scripts from the command line interface. PHP is a general-purpose scripting language well-suited for web development and easily integrated with HTML. The php command provides an easy way to execute PHP code without setting up a web server.
Table of Contents
Here is a comprehensive guide to the options available with the php command â
- Installation of php Command in Linux
- Syntax of php Command
- php Command Options
- Examples of php Command in Linux
Installation of php Command in Linux
To use the php command in Linux the PHP must be installed. To install PHP on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and Debian-based distributions, use the following command −
sudo apt install php
To install PHP on Arch Linux, use the following command −
sudo pacman -S php
To install PHP on CentOS, use the command given below −
sudo yum install php-cli
To install it on Fedora, use the following command −
sudo dnf install php-cli
To verify the php command installation, check its version −
php --version

Syntax of php Command
The syntax of the php command in Linux is as follows −
php [options] [args]
In the above syntax, the [options] field is used to specify the flags or parameters to modify the behavior of the PHP interpreter. The [args] field is used to specify the inputs or additional parameters passed to the script being executed.
php Command Options
The options of the Linux php command are below −
Flag | Option | Description |
---|---|---|
-a | --interactive | Run PHP interactively, enabling direct code execution with line editing and history support if readline is enabled. |
-C | --no-chdir | Avoid changing the directory to the script's location (CGI only). |
-q | --no-header | Enable quiet mode. Suppress HTTP header output (CGI only). |
-c path|file | --php-ini path|file | Specify the directory or file to search for php.ini. |
-n | --no-php-ini | Disable the use of any php.ini file. |
-d foo[=bar] | --define foo[=bar] | Define an INI entry with the specified value. |
-e | Generate extended information for debugging or profiling. | |
-f file | --file file | Parse and execute a specified file. |
-h | --help | Display help information. |
-H | --hide-args | Hide the script name and arguments from external tools for security reasons. |
-i | --info | Display PHP configuration and information. |
-m | --modules | List compiled-in modules. |
-r code | --run code | Execute PHP code without using script tags. |
-B code | --process-begin code | Execute PHP code before processing input lines. |
-R code | --process-code code | Execute PHP code for every input line. |
-F file | --process-file file | Parse and execute a file for every input line. |
-E code | --process-end code | Execute PHP code after processing all input lines. |
-s | --syntax-highlight | Output syntax-highlighted HTML source. |
-t | --docroot | Define the document root for the built-in web server. |
-v | --version | Display the PHP version number. |
-w | --stripped | Output source code with comments and whitespace removed. |
-z file | --zend-extension file | Load a Zend extension from the specified file. |
--rf / --rfunction name | Display information about a specified function. | |
--rc / --rclass name | Display information about a specified class. | |
--rx / --rextension name | Display information about a specified extension. | |
--ri / --rextinfo name | Show configuration for a specified extension. | |
--ini | Display configuration file names. |
Examples of php Command in Linux
In this section, the usage of the php command in Linux will be discussed with examples −
- Running PHP Interactively in Linux
- Executing a PHP Script
- Executing a PHP Script from the Command Line
- Running PHP Quietly
- Specifying the Custom php.ini File
- Defining the INI for the Script
- Displaying PHP Configuration
- Displaying the Compiled Modules
- Generating the HTML Syntax Highlighted Source
- Displaying PHP Script without Comments and Whitespaces
- Displaying the Configuration File Names
- Displaying Usage Help
Running PHP Interactively in Linux
To start the PHP interactive shell, use the php command with -a or --interactive option −
php -a

The above command starts an interactive PHP shell PHP code can be typed and executed as shown in the image below −

Executing a PHP Script
To execute a PHP script, use the -f or --file option with the script file −
php -f script.php

However, this flag is optional, the script can also be executed without specifying the flag.

Executing a PHP Script from the Command Line
To execute the PHP script from the command line instead of the file, use the -r or --run option with code −
php -r 'echo "Hello, PHP!";'

Running PHP Quietly
To execute the PHP script quietly, use the -q or --no-header option with the php command −
php -q script.php
The above command suppresses the HTTP header output.
Specifying the Custom php.ini File
The php.ini file is a configuration file used by PHP to control various aspects of its behavior. By default, it is located in multiple places, depending upon the distribution.
To specify the custom php.ini file, use the -c or --php-ini option −
php -c /path/to/php.ini script.php
Defining the INI for the Script
To define the inline INI entry for the script, use the -d or --define option with the INI directive and value. For example, to enable the display errors during the execution of the specified script, use the following command −
php -d display_errors=1 script.php
Displaying PHP Configuration
To display the PHP configuration, use the -i or --info option −
php -i

Displaying the Compiled Modules
To display all the compiled modules for PHP, use the -m or --modules option −
php -m

These are the PHP extensions that are available in PHP installation.
Generating the HTML Syntax Highlighted Source
To display the HTML syntax highlighted source, use the -s or --syntax-highlight option with the php command −
php -s script.php

This syntax-highlighted HTML output is useful for displaying PHP code on web pages, documentation, or tutorials, enhancing readability by using colors to distinguish different code elements.
Displaying PHP Script without Comments and Whitespaces
To display the PHP script without comments and whitespaces, use the -w or --stripped option with the php command −
php -w script.php

Displaying the Configuration File Names
To display the locations of the php.ini configuration files, use the --ini option −
php --ini

Displaying Usage Help
To display the usage help of the php command, use the -h or --help option −
php -h
Conclusion
The php command in Linux allows easy execution of PHP scripts directly from the command line, making it a handy tool for testing and running scripts without needing a web server. It provides various options for controlling script execution, such as running PHP interactively, specifying custom configuration files, or displaying information about installed modules. Additionally, the behavior of PHP scripts can be modified using different flags, such as for quiet execution, syntax highlighting, or stripping comments from the source code.
In this tutorial, we covered the php command, its installation, syntax, options, and usage in Linux with examples.