F:/phpStudy/php53/php.exe -f F:/phpStudy/WWW/qh/qh.php
/usr/local/php/bin/php -f test.php
Usage: php [options] [-f] <file> [--] [args...] php [options] -r <code> [--] [args...] php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...] php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...] php [options] -- [args...] php [options] -a -a Run interactively -c <path>|<file> Look for php.ini file in this directory -n No php.ini file will be used -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/PRofiler -f <file> Parse <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -s Display colour syntax highlighted source. -v Version number -w Display source with stripped comments and whitespace. -z <file> Load Zend extension <file>. args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdinhttp://www.php100.com/manual/php/features.commandline.htmlhttp://chenpeng.info/html/2129?utm_source=tuicool&utm_medium=referralPHP 的命令行模式 CLI參數(shù)
一些php異常在LAMP環(huán)境會(huì)導(dǎo)致php執(zhí)行的中斷,沒法捕獲,這時(shí)候用PHP CLI來執(zhí)行能看到更詳細(xì)的錯(cuò)誤信息。
為了減輕外殼環(huán)境下的工作,我們定義了如下常量:
| 常量名稱 | 描 述 |
|---|---|
STDIN | 一個(gè)已打開的指向 stdin 的流。可以用如下方法來調(diào)用:
$stdin = fopen('php://stdin', 'r');
如果想從 stdin 讀取一行內(nèi)容,可以使用 <?php$line = trim(fgets(STDIN)); // 從 STDIN 讀取一行fscanf(STDIN, "%d/n", $number); // 從 STDIN 讀取數(shù)字?> |
STDOUT | 一個(gè)已打開的指向 stdout 的流。可以用如下方式來調(diào)用:
$stdout = fopen('php://stdout', 'w');
|
STDERR | 一個(gè)已打開的指向 stderr 的流。可以用如下方式來調(diào)用:
$stderr = fopen('php://stderr', 'w');
|
有了以上常量,就無需自己建立指向諸如 stderr 的流,只需簡單的使用這些常量來代替流指向:
php -r 'fwrite(STDERR, "stderr/n");'無需自己來關(guān)閉這些流,PHP 會(huì)自動(dòng)完成這些操作。
CLI SAPI 不會(huì)將當(dāng)前目錄改為已運(yùn)行的腳本所在的目錄。
以下范例顯示了本模塊與 CGI SAPI 模塊之間的不同:
<?php// 名為 test.php 的簡單測試程序echo getcwd(), "/n";?>在使用 CGI 版本時(shí),其輸出為
$ pwd/tmp$ php-cgi -f another_directory/test.php/tmp/another_directory明顯可以看到 PHP 將當(dāng)前目錄改成了剛剛運(yùn)行過的腳本所在的目錄。
使用 CLI SAPI 模式,得到:
$ pwd/tmp$ php -q another_directory/test.php/tmp這使得在利用 PHP 編寫外殼工具時(shí)獲得了很大的便利。
Note:
可以在命令行運(yùn)行時(shí)給該 CGI SAPI 加上 -C 參數(shù),使其支持 CLI SAPI 的功能。
以下是 PHP 二進(jìn)制文件(即 php.exe 程序)提供的命令行模式的選項(xiàng)參數(shù),隨時(shí)可以運(yùn)行帶 -h 參數(shù)的 PHP 命令來查詢這些參數(shù)。
Usage: php [options] [-f] <file> [--] [args...] php [options] -r <code> [--] [args...] php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...] php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...] php [options] -- [args...] php [options] -a -a Run interactively -c <path>|<file> Look for php.ini file in this directory -n No php.ini file will be used -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -f <file> Parse <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -s Display colour syntax highlighted source. -v Version number -w Display source with stripped comments and whitespace. -z <file> Load Zend extension <file>. args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdinCLI SAPI 模塊有以下三種不同的方法來獲取要運(yùn)行的 PHP 代碼:
讓 PHP 運(yùn)行指定文件。
php my_script.phpphp -f my_script.php以上兩種方法(使用或不使用 -f 參數(shù))都能夠運(yùn)行給定的 my_script.php 文件。可以選擇任何文件來運(yùn)行,指定的 PHP 腳本并非必須要以 .php 為擴(kuò)展名,它們可以有任意的文件名和擴(kuò)展名。
在命令行直接運(yùn)行 PHP 代碼。
php -r 'print_r(get_defined_constants());'在使用這種方法時(shí),請注意外殼變量的替代及引號的使用。
Note:
請仔細(xì)閱讀以上范例,在運(yùn)行代碼時(shí)沒有開始和結(jié)束的標(biāo)記符!加上 -r 參數(shù)后,這些標(biāo)記符是不需要的,加上它們會(huì)導(dǎo)致語法錯(cuò)誤。
通過標(biāo)準(zhǔn)輸入(stdin)提供需要運(yùn)行的 PHP 代碼。
以上用法提供了非常強(qiáng)大的功能,使得可以如下范例所示,動(dòng)態(tài)地生成 PHP 代碼并通過命令行運(yùn)行這些代碼:
$ some_application | some_filter | php | sort -u >final_output.txt以上三種運(yùn)行代碼的方法不能同時(shí)使用。
和所有的外殼應(yīng)用程序一樣,PHP 的二進(jìn)制文件(php.exe 文件)及其運(yùn)行的 PHP 腳本能夠接受一系列的參數(shù)。PHP 沒有限制傳送給腳本程序的參數(shù)的個(gè)數(shù)(外殼程序?qū)γ钚械淖址麛?shù)有限制,但通常都不會(huì)超過該限制)。傳遞給腳本的參數(shù)可在全局變量 $argv 中獲取。該數(shù)組中下標(biāo)為零的成員為腳本的名稱(當(dāng) PHP 代碼來自標(biāo)準(zhǔn)輸入獲直接用 -r 參數(shù)以命令行方式運(yùn)行時(shí),該名稱為“–”)。另外,全局變量 $argc 存有 $argv 數(shù)組中成員變量的個(gè)數(shù)(而非傳送給腳本程序的參數(shù)的個(gè)數(shù))。
只要傳送給腳本的參數(shù)不是以 – 符號開頭,就無需過多的注意什么。向腳本傳送以 – 開頭的參數(shù)會(huì)導(dǎo)致錯(cuò)誤,因?yàn)?PHP 會(huì)認(rèn)為應(yīng)該由它自身來處理這些參數(shù)。可以用參數(shù)列表分隔符 — 來解決這個(gè)問題。在 PHP 解析完參數(shù)后,該符號后所有的參數(shù)將會(huì)被原樣傳送給腳本程序。
# 以下命令將不會(huì)運(yùn)行 PHP 代碼,而只顯示 PHP 命令行模式的使用說明:$ php -r 'var_dump($argv);' -hUsage: php [options] [-f] <file> [args...][...]# 以下命令將會(huì)把“-h”參數(shù)傳送給腳本程序,PHP 不會(huì)顯示命令行模式的使用說明:$ php -r 'var_dump($argv);' -- -harray(2) { [0]=> string(1) "-" [1]=> string(2) "-h"}除此之外,還有另一個(gè)方法將 PHP 用于外殼腳本。可以在寫一個(gè)腳本,并在第一行以 #!/usr/bin/php 開頭,在其后加上以 PHP 開始和結(jié)尾標(biāo)記符包含的正常的 PHP 代碼,然后為該文件設(shè)置正確的運(yùn)行屬性(例如:chmod +x test)。該方法可以使得該文件能夠像外殼腳本或 PERL 腳本一樣被直接執(zhí)行。
#!/usr/bin/php<?php var_dump($argv);?>假設(shè)改文件名為 test 并被放置在當(dāng)前目錄下,可以做如下操作:
$ chmod +x test$ ./test -h -- fooarray(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo"}正如所看到的,在向該腳本傳送以 – 開頭的參數(shù)時(shí),腳本仍然能夠正常運(yùn)行。
PHP 4.3.3 以來有效的長選項(xiàng):
命令行選項(xiàng) 選項(xiàng)名稱 長名稱 說明 -a –interactive 交互式運(yùn)行 PHP。如果編譯 PHP 時(shí)加入了 Readline 擴(kuò)展(Windows 下不可用),那將會(huì)得到一個(gè)很好的外殼,包括一個(gè)自動(dòng)完成的功能(例如可以在鍵入變量名的時(shí)候,按下 TAB 鍵,PHP 會(huì)自動(dòng)完成該變量名)以及命令歷史記錄,可以用上下鍵來訪問。歷史記錄存在 ~/.php_history 文件中。
Note:
通過 auto_prepend_file 和 auto_append_file 包含的文件在此模式下會(huì)被解析,但有些限制,例如函數(shù)必須在被調(diào)用之前定義。
-c –php-ini 用該參數(shù),可以指定一個(gè)放置 php.ini 文件的目錄,或者直接指定一個(gè)自定義的 INI 文件(其文件名可以不是 php.ini),例如:
$ php -c /custom/directory/ my_script.php$ php -c /custom/directory/custom-file.ini my_script.php如果不指定此選項(xiàng),PHP 將在默認(rèn)位置搜索文件。
-n –no-php-ini 完全忽略 php.ini。此參數(shù)在 PHP 4.3.0 以后有效。
-d –define 用該參數(shù)可以自行設(shè)置任何可以在 php.ini 文件中設(shè)置的配置選項(xiàng)的值,其語法為:
-d configuration_directive[=value]例子(因版面原因而折行顯示):
# 取值部分被省略,將會(huì)把配置選項(xiàng)設(shè)為 "1"$ php -d max_execution_time -r '$foo = ini_get("max_execution_time"); var_dump($foo);'string(1) "1"# 取值部分為空白,將會(huì)把配置選項(xiàng)設(shè)為 ""php -d max_execution_time= -r '$foo = ini_get("max_execution_time"); var_dump($foo);'string(0) ""# 配置選項(xiàng)將被設(shè)置成為任何 '=' 字符之后的值$ php -d max_execution_time=20 -r '$foo = ini_get("max_execution_time"); var_dump($foo);'string(2) "20"$ php -d max_execution_time=doesntmakesense -r '$foo = ini_get("max_execution_time"); var_dump($foo);'string(15) "doesntmakesense"-e –profile-info 激活擴(kuò)展信息模式,被用于調(diào)試/測試。
-f –file 解析并運(yùn)行 -f 選項(xiàng)給定的文件名。該參數(shù)為可選參數(shù),可以省略,僅指明需要運(yùn)行的文件名即可。
-h and -? –help and –usage 使用該參數(shù),可以得到完整的命令行參數(shù)的列表及這些參數(shù)作用的簡單描述。 -i –info 該命令行參數(shù)會(huì)調(diào)用 phpinfo() 函數(shù)并顯示出結(jié)果。如果 PHP 沒有正常工作,建議執(zhí)行 php -i 命令來查看在信息表格之前或者對應(yīng)的地方是否有任何錯(cuò)誤信息輸出。請注意當(dāng)使用 CGI 摸索時(shí),輸出的內(nèi)容為 HTML 格式,因此輸出的信息篇幅較大。 -l –syntax-check 該參數(shù)提供了對指定 PHP 代碼進(jìn)行語法檢查的方便的方法。如果成功,則向標(biāo)準(zhǔn)輸出寫入No syntax errors detected in <filename> 字符串,并且外殼返回值為 0。如果失敗,則輸出Errors parsing <filename> 以及內(nèi)部解析器錯(cuò)誤信息到標(biāo)準(zhǔn)輸出,同時(shí)外殼返回值將別設(shè)置為255。
該參數(shù)將無法檢查致命錯(cuò)誤(如未定義函數(shù)),如果也希望檢測致命錯(cuò)誤,請使用 -f 參數(shù)。
Note:
該參數(shù)不能和 -r 一同使用。
-m –modules 使用該參數(shù),PHP 將打印出內(nèi)置以及已加載的 PHP 及 Zend 模塊:
$ php -m[PHP Modules]xmltokenizerstandardsessionposixpcreoverloadMySQLmbstringctype[Zend Modules]-r –run 使用該參數(shù)可以在命令行內(nèi)運(yùn)行單行 PHP 代碼。無需加上 PHP 的起始和結(jié)束標(biāo)識符(<?php 和?>),否則將會(huì)導(dǎo)致語法解析錯(cuò)誤。
Note:
使用這種形式的 PHP 時(shí),應(yīng)注意避免和外殼環(huán)境進(jìn)行的命令行參數(shù)替換相沖突。
顯示語法解析錯(cuò)誤的范例
$ php -r "$foo = get_defined_constants();"Command line code(1) : Parse error - parse error, unexpected '='這里的問題在于即使使用了雙引號 “,sh/bash 仍然實(shí)行了參數(shù)替換。由于 $foo 沒有被定義,被替換后它所在的位置變成了空字符,因此在運(yùn)行時(shí),實(shí)際被 PHP 讀取的代碼為:
$ php -r " = get_defined_constants();"正確的方法是使用單引號 ‘。在用單引號引用的字符串中,變量不會(huì)被 sh/bash 還原成其原值。
$ php -r '$foo = get_defined_constants(); var_dump($foo);'array(370) { ["E_ERROR"]=> int(1) ["E_WARNING"]=> int(2) ["E_PARSE"]=> int(4) ["E_NOTICE"]=> int(8) ["E_CORE_ERROR"]=> [...]如果使用的外殼不是 sh/bash,可能會(huì)碰到更多問題。請將碰到的 Bug 向? http://bugs.php.net/ 報(bào)告。注意,當(dāng)試圖將 shell 變量用到代碼中或者使用反斜線時(shí)仍然很容易碰到問題。
Note:
-r 在 CLI SAPI 中有效,在 CGI SAPI 中無效。
Note:
此選項(xiàng)只用于非常基本的用途。因此一些配置指令(例如 auto_prepend_file 和auto_append_file)在此模式下被忽略。
-B –process-begin 在處理 stdin 之前先執(zhí)行 PHP 代碼。PHP 5 新加。
-R –process-code 對每個(gè)輸入行都執(zhí)行 PHP 代碼。PHP 5 新加。
此模式下有兩個(gè)特殊變量:$argn 和 $argi。$argn 包含 PHP 當(dāng)前處理的行內(nèi)容,而$argi 則包含該行號。
-F –process-file 對每個(gè)輸入行都執(zhí)行 PHP 文件。PHP 5 新加。
-E –process-end 在處理完輸入后執(zhí)行的 PHP 代碼。PHP 5 新加。
使用 -B ,-R 和 -E 選項(xiàng)來計(jì)算一個(gè)項(xiàng)目總行數(shù)的例子。
$ find my_proj | php -B '$l=0;' -R '$l += count(@file($argn));' -E 'echo "Total Lines: $l/n";'Total Lines: 37328-s –syntax-highlight and –syntax-highlight 顯示有語法高亮色彩的源代碼。
該參數(shù)使用內(nèi)建機(jī)制來解析文件并為其生成一個(gè) HTML 高亮版本并將結(jié)果寫到標(biāo)準(zhǔn)輸出。請注意該過程所做的只是生成了一個(gè) <code> […] </code> 的 HTML 標(biāo)記的塊,并不包含任何的 HTML頭。
Note:
該選項(xiàng)不能和 -r 參數(shù)同時(shí)使用。
-v –version 將 PHP,PHP SAPI 和 Zend 的版本信息寫入標(biāo)準(zhǔn)輸出。例如:
$ php -vPHP 4.3.0 (cli), Copyright (c) 1997-2002 The PHP GroupZend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies-w –strip 顯示除去了注釋和多余空白的源代碼。
Note:
該選項(xiàng)不能和 -r 參數(shù)同時(shí)使用。
-z –zend-extension 加載 Zend 擴(kuò)展庫。如果僅給定一個(gè)文件名,PHP 將試圖從當(dāng)前系統(tǒng)擴(kuò)展庫的默認(rèn)路徑(在 linux 系統(tǒng)下,該路徑通常由 /etc/ld.so.conf 指定)加載該擴(kuò)展庫。如果用一個(gè)絕對路徑指定文件名,則不會(huì)使用系統(tǒng)的擴(kuò)展庫默認(rèn)路徑。如果用相對路徑指定的文件名,則 PHP 僅試圖在當(dāng)前目錄的相對目錄加載擴(kuò)展庫。
PHP 的命令行模式能使得 PHP 腳本能完全獨(dú)立于 web 服務(wù)器單獨(dú)運(yùn)行。如果使用 Unix 系統(tǒng),需要在 PHP 腳本的最前面加上一行特殊的代碼,使得它能夠被執(zhí)行,這樣系統(tǒng)就能知道用哪個(gè)程序去運(yùn)行該腳本。在 Windows 平臺下可以將php.exe 和 .php 文件的雙擊屬性相關(guān)聯(lián),也可以編寫一個(gè)批處理文件來用 PHP 執(zhí)行腳本。為 Unix 系統(tǒng)增加的第一行代碼不會(huì)影響該腳本在 Windows 下的運(yùn)行,因此也可以用該方法編寫跨平臺的腳本程序。以下是一個(gè)簡單的 PHP 命令行程序的范例。
Example #1 試圖以命令行方式運(yùn)行的 PHP 腳本(script.php)
#!/usr/bin/php<?php
if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {?>This is a command line PHP script with one option.
Usage: <?php echo $argv[0]; ?> <option>
<option> can be some Word you would like to print out. With the --help, -help, -h, or -? options, you can get this help.
<?php} else { echo $argv[1];}?>在以上腳本中,用第一行特殊的代碼來指明該文件應(yīng)該由 PHP 來執(zhí)行。在這里使用 CLI 的版本,因此不會(huì)有 HTTP 頭信息輸出。在用 PHP 編寫命令行應(yīng)用程序時(shí),可以使用兩個(gè)參數(shù):$argc 和 $argv。前面一個(gè)的值是比參數(shù)個(gè)數(shù)大 1 的整數(shù)(運(yùn)行的腳本本身的名稱也被當(dāng)作一個(gè)參數(shù))。第二個(gè)是包含有參數(shù)的數(shù)組,其第一個(gè)元素為腳本的名稱,下標(biāo)為數(shù)字 0($argv[0])。
以上程序中檢查了參數(shù)的個(gè)數(shù)是大于 1 個(gè)還是小于 1 個(gè)。此外如果參數(shù)是 –help ,-help ,-h 或 -? 時(shí),打印出幫助信息,并同時(shí)動(dòng)態(tài)輸出腳本的名稱。如果還收到了其它參數(shù),將其顯示出來。
如果希望在 Unix 下運(yùn)行以上腳本,需要使其屬性為可執(zhí)行文件,然后簡單的運(yùn)行 script.php echothis 或 script.php -h。在 Windows 下,可以為此編寫一個(gè)批處理文件:
Example #2 運(yùn)行 PHP 命令行腳本的批處理文件(script.bat)
@C:/php/php.exe script.php %1 %2 %3 %假設(shè)將上述程序命名為 script.php,且 CLI 版的 php.exe 文件放置在 c:/php/cli/php.exe,該批處理文件會(huì)幫助將附加的參數(shù)傳給腳本程序:script.bat echothis 或 script.bat -h。
新聞熱點(diǎn)
疑難解答