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
- PHP 5.2. (CLI) (Built:feb 2 :)
- Copyright (c) 1997-thePHP Group
- Zend Engine v2. 2.0, Copyright (c) 1998-Zend Technologies
- With Zend Optimizer v3. 3.9, Copyright (c) 1998-, by Zend Technologies
- 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
-
- echo "Run PHP command line echo";
- ?>
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
- -bash-3.2$ php-r '
- > For ($i =0; $i <2; $i + +) {
- > Echo "number: {$i}\n";
- >}
- > '
- Number: 0
- 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
- -bash-3.2$ php-l hello.php
- No syntax errors detected in hello.php
Programmers often need to highlight the PHP code as it is, using Php–s to
Java code
- -bash-3.2$ php-s hello.php
"color: #000000">
"color: #0000BB"><?php
"color: #007700">echo
"color: #DD0000">'ddd'
"color: #007700">;
< span="">
style="color: #0000BB">?>
在浏览器中的显示效果
4、查看php手册
从php5.1.2开始,程序员们可以在php命令行下查看手册了,输入php –rf function。会打印出该函数的语法简介
Java代码
-bash-3.2$ php --rf strip_tags
Function [ function strip_tags ] {
- Parameters [2] {
Parameter #0 [ $str ]
Parameter #1 [ $allowable_tags ]
}
}
如果要查看类使用 –rc;查看扩展使用 –re。
Php命令行的使用方法介绍到这里就结束了。Enjoy It!
来源:http://www.cnphp.info/php-command-line-useage-intro.html
更多:http://www.tutorialspoint.com/unix_commands/php.htm
php - Unix, Linux Command
NAME
php - PHP Command Line Interface ’CLI’
SYNOPSIS
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
DESCRIPTION
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. This is the command line interface that enables you to do the following:
You can parse and execute files by using parameter -f followed by the name of the file to be executed.
Using parameter -r you can directly execute PHP code simply as you would do inside a .php file when using theeval() function.
It is also possible to process the standard input line by 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 be used to execute code (see -r) before and after all input lines have been processed respectively. Notice that the input is read from STDIN and therefore reading from STDIN explicitly changes the next input line or skips input lines.
If none of -r -f -B -R -F or -E is present but a single parameter is given then this parameter is 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.
OPTIONS
Tag Description
--interactive -A |
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. |
--bindpathAddress:port| Port -BAddress:port| Port |
|
Bind Path for external FASTCGI Server mode (CGI-only). |
--no-chdir -C |
Don't chdir to the script ' s directory (CGI only). |
--no-header -Q |
Quiet-mode. Suppress HTTP header output (CGI only). |
--timing Count -T Count |
Measure execution time of script repeated count times (CGI only). |
--php-iniPath| file -C Path| file |
Look for php.ini file in the directory path or use the specified file |
--no-php-ini -N |
No php.ini file would be used |
--definefoo[=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. |
--help -H |
This Help |
--hide-args -H |
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. |
--info -I. |
PHP Information and configuration |
--syntax-check -L |
Syntax Check Only (lint) |
--modules -M |
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-codeCode -R Code |
|
Run PHP Code for every input line |
--process-fileFile -F File |
|
Parse and execute file for every input line |
--process-endCode -E Code |
Run PHP code after processing all input lines |
--syntax-highlight -S |
Output HTML Syntax highlighted source |
--version -V |
Version number |
--stripped -W |
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 |
FILES
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. |
EXAMPLES
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. |
TIPS
You can use a shebang line to automatically invoke php from scripts. Only the CLI version of PHP will ignore such a first line as shown below:
#!/bin/php
// your script
?>
SEE ALSO
http://www.php.net/manual/
BUGS
You can view the list of known bugs or report any new bug you found at:
http://bugs.php.net
AUTHORS
The PHP Group: Thies C. Arntzen, Stig Bakken, Andi Gutmans, Rasmus Lerdorf, Sam Ruby, Sascha Schumann, Zeev Suraski, Jim Winstead, Andrei Zmievski.
Additional work for the CLI sapi was done by Edin Kadribasic, Marcus Boerger and Johannes Schlueter.
A List of active developers can be found here:
http://www.php.net/credits.php
And last but not least PHP was developed with the help of a huge amount of contributors all around the world.
VERSION INFORMATION
This manpage describes php, version 5.2.17.
COPYRIGHT
Copyright © 1997-2010 The PHP Group
以上就介绍了php命令行用法简介,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。