Introduction to PHP command line usage, PHP command line usage _php Tutorial

Source: Internet
Author: User
Tags stdin

Introduction to PHP command line usage, PHP command line usage

PHP is a very popular web service-side scripting language. In fact, PHP can not only play an important role in the Web server. Can be executed at the command line.

In this article, I will introduce you to PHP in the command line of the use of the method .

1. View PHP version, configuration

You can view the current version of PHP by entering Php–v on the command line.

Java code
    1. PHP 5.2. (CLI) (Built:feb 2 :)
    2. Copyright (c) 1997-thePHP Group
    3. Zend Engine v2. 2.0, Copyright (c) 1998-Zend Technologies
    4. With Zend Optimizer v3. 3.9, Copyright (c) 1998-, by Zend Technologies
    5. With Eaccelerator V0. 9.6.1, Copyright (c) 2004-eaccelerator, by Eaccelerator

Other options are: –m,-I. The author here will not give out the son.

-M displays the active module for the current PHP load.

-I outputs a phpinfo without HTML format.

Use the –ini option to output the number and path information for the current PHP load INI configuration file.

2. Run the PHP program on the command line

Running PHP from the command line is easy. But there are some caveats that you need to know. Server variables such as $_session cannot be used on the command line, and other code runs exactly as ^_^ in the Web server.

PHP code
    1. echo "Run PHP command line echo";
    2. ?>

Save the above code as hello.php. In the command line, Php–f hello.php is typed. The results appear as follows:

One of the benefits of executing PHP files on the command line is that you can implement some of the scheduled tasks through scripting. Without the need to ^_^ through the Web server.

Of course, we can also debug the code directly in PHP: Enter the php–r instruction, a ">" symbol appears. This means that you have entered the shell of PHP and can write the code directly and execute it.

Java code
    1. -bash-3.2$ php-r '
    2. > For ($i =0; $i <2; $i + +) {
    3. > Echo "number: {$i}\n";
    4. >}
    5. > '
    6. Number: 0
    7. Number: 1

You can also use the Php–a command to open interactive mode, enter a line of code, PHP will output the results in real time.

3. Check php syntax, highlight output

Instead of executing code, we can detect PHP file syntax errors at the command line.

Java code
    1. -bash-3.2$ php-l hello.php
    2. No syntax errors detected in hello.php

Programmers often need to highlight the PHP code as it is, using Php–s to

Java code
    1. -bash-3.2$ php-s hello.php
    2. "color: #000000">
    3. "color: #0000BB"><?php
    4. "color: #007700">echo
    5. "color: #DD0000">'ddd'
    6. "color: #007700">;
      < span="">
    7. style="color: #0000BB">?>



从php5.1.2开始,程序员们可以在php命令行下查看手册了,输入php –rf function。会打印出该函数的语法简介

Java code
    1. -bash-3.2$ php--rf strip_tags
    2. function [ function Strip_tags] {
    3. -Parameters [2] {
    4. Parameter #0 [ $str]
    5. Parameter #1 [ $allowable _tags]
    6. }
    7. }

If you want to view classes using –RC, see Extensions using –re.

Introduction to the use of the PHP command line here is the end. Enjoy it!



Php-unix, Linux Command


php-php Command line Interface ' CLI '


PHP [Options] [- F ] file [[--] args ...]

PHP [Options] - R Code [[--] args ...]

PHP [Options] [-B Code] - R Code [-e code] [[--] args ...]

PHP [Options] [-B Code] - F file [-e code] [[--] args ...]

PHP [Options]--[ args...]

PHP [Options] -A


PHP is a widely-used general-purpose scripting language that's especially suited for Web development and can be Embedded into HTML. This is the command line interface that enables the following:

You can parse and execute files by using parameter-f followed by the name of the file to is executed.

Using Parameter-r You can directly execute PHP code simply as you would does inside a . PHP file when usin G Theeval () function.

It is also possible to process, the standard input line through line using either the Parameter-r or-f. In this mode each separate input line causes the code specified by-r or the file specified by-f to be Executed. You can access the input line by $argn. While processing the input lines $argi contains the number of the actual line being processed. Further more the Parameters-b and-e can is used to execute code (SEE-R) before and after all input lines has been processed respectively. Notice that the input was read from STDIN and therefore reading from STDIN explicitly changes the next in Put line or skips input lines.

If None Of-r-f-b-r-f or-e is present, a single parameter was given then this parameter was taken as the filename to Parse and execute (same as with-f). If no parameter is present then the "standard" input is read and executed.


Tag Description

Run PHP interactively. This lets your enter snippets of PHP code that directly get executed. When ReadLine support was enabled you can edit the lines and also has a history of support.
--bindpath Address:port| Port
- b Address:port| Port
Bind Path for external FASTCGI Server mode (CGI-only).
Don't chdir to the script ' s directory (CGI only).
Quiet-mode. Suppress HTTP header output (CGI only).
--timing Count
- T Count
Measure execution time of script repeated count times (CGI only).
--php-ini Path| file
- C Path| file
Look for php.ini file in the directory path or use the specified file
No php.ini file would be used
--define Foo [=Bar]
- D Foo [=Bar]
Define INI Entry foo with value bar
-E Generate Extended information for Debugger/profiler
--file file
- F file
Parse and execute file
--global name
- G name
Make variable name Global in script.
This Help
Hide Script Name (file) and parameters (args ...) from external tools. For example-want to use the If a PHP script is started as a daemon and the command line contains sensitive data such as passwords.
PHP Information and configuration
Syntax Check Only (lint)
Show compiled in Modules
--run Code
- R Code
Run PHP code without using script tags '
--process-begin Code
- b Code
Run PHP code before processing input lines
--process-code Code
- R Code
Run PHP Code for every input line
--process-file file
- F file
Parse and execute file for every input line
--process-end Code
- e Code
Run PHP code after processing all input lines
Output HTML Syntax highlighted source
Version number
Output source with stripped comments and whitespace
--zend-extension file
- Z file
Load Zend extension file
Args ... Arguments passed to script. Use '--' the args when first argument starts with '-' or script was read from stdin
--rfunction Name
--rf name Shows information about function name
--rclass Name
--rc name Shows information about class name
--rextension Name
--re name Shows information about extension name
--rextinfo Name
--ri name Shows configuration for extension name
--ini Show configuration file names


Tag Description

Php-cli.ini The configuration file for the CLI version of PHP.
Ini The standard configuration file is only being used when Php-cli.ini cannot is found.


Tag Description

Php-r ' echo ' Hello world\n '; '
This command simply writes the text ' Hello World ' to standard out.
Php-r ' Print_r (Gd_info ()); '
This shows the configuration of your GD extension. Easily check which image formats you can use. If you had any of the dynamic modules want to use the same INI file, PHP uses when executed from your webserver. There is more extensions which has such a function. For DBA use:php-r ' Print_r (dba_handlers (1)); '
Php-r ' echo strip_tags ($ARGN). \ n ";"
This PHP command strips off the HTML tags line by line and outputs the result. To see how it works can first look at the following PHP command 'php-d html_errors=1-i' which uses PHP to O Utput HTML formatted configuration information. If You then combine those "php ... | php ...' You'll see what happens.
Php-e ' echo ' Lines: $argi \ n ";
Using This PHP command, you can count the lines being input.
Php-r ' @ $l +=count (file ($argn)), '-E ' echo ' Lines: $l \ n ';
In this example PHP expects all input line being a file. It counts all lines of the files specified by each input line and shows the summarized result. Combine this with tools like find and change the PHP scriptlet.
Php-r ' echo ' $argn \ n '; Fgets (STDIN); '
Since you has access to STDIN from Within-b-r-f and-e You can skip certain input lines with your code. But note this in such cases $argi only counts the lines being processed by PHP itself. Have read this you'll guess what the above program does:skipping every second input line.


You can use a shebang line to automatically invoke PHP from scripts. Only the CLI version of PHP would ignore such a first line as shown below:


Your script




You can view the list of known bugs or report any new bugs you found at:



The PHP group:thies C. Arntzen, Stig Bakken, Andi Gutmans, Rasmus Lerdorf, Sam Ruby, Sascha Schumann, Zeev Suraski, Jim W Instead, Andrei Zmievski.

Additional work for the CLI SAPI is done by Edin Kadribasic, Marcus Boerger and Johannes schlueter.

A List of active developers can is found here:

And last but not least PHP is developed with the help's a huge amount of contributors all around the world.

VERSION Information

This manpage describes php, version 5.2.17.


copyright©1997-2010 the PHP Group true techarticle Introduction to PHP command-line usage, PHP command-line usage PHP is a very popular Web server scripting language. In fact, PHP can not only play an important role in the Web server. At the command line ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.