CLI模式
CLI模式其實就是命令行運行模式,英文全稱Command-Line Interface(命令行接口)
$ php -hUsage: 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] -S addr : port [-t docroot] [router] php [options] -- [args...] php [options] -a -a Run as interactive shell 以交互shell模式運行 -c path | file Look for php.ini file in this directory 指定php.ini文件所在的目錄 -n No configuration (ini) files will be used 指定不使用php.ini文件 -d foo[=bar] Define INI entry foo with value bar 定義一個INI實體,key為foo,value為 bar -e Generate extended information for debugger/profiler 為調試和分析生成擴展信息 -f file Parse and execute file . 解釋和執行文件 file -h This help 打印幫助信息 -i PHP information 顯示PHP的基本信息 -l Syntax check only (lint) 進行語法檢查(lint) -m Show compiled in modules 顯示編譯到內核的模塊 -r code Run PHP code without using script tags ?..? 運行PHP代碼 code ,不需要使用標簽 ?..? -B begin_code Run PHP begin_code before processing input lines 在處理輸入之前先執行PHP代碼 begin_code -R code Run PHP code for every input line 對輸入的每一行作為PHP代碼 code 運行 -F file Parse and execute file for every input line 對輸入的每一行解析和執行 file -E end_code Run PHP end_code after processing all input lines 在處理所有輸入的行之后執行PHP代碼 end_code -H Hide any passed arguments from external tools. 隱藏任何來自外部工具傳遞的參數 -S addr : port Run with built-in web server. 運行內置的web服務器 -t docroot Specify document root docroot for built-in web server. 指定用于內置web服務器的文檔根目錄 docroot -s Output HTML syntax highlighted source. 輸出HTML語法高亮的源碼 -v Version number 輸出PHP的版本號 -w Output source with stripped comments and whitespace. 輸出去掉注釋和空格的源碼 -z file Load Zend extension file . 載入Zend擴展文件 file args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin 傳遞給要運行的腳本的參數。當第一個參數以 - 開始或者是腳本是從標準輸入讀取的時候,使用 -- 參數 --ini Show configuration file names 顯示PHP的配置文件名 --rf name Show information about function name . 顯示關于函數 name 的信息 --rc name Show information about class name . 顯示關于類 name 的信息 --re name Show information about extension name . 顯示關于擴展 name 的信息 --rz name Show information about Zend extension name . 顯示關于Zend擴展 name 的信息 --ri name Show configuration for extension name . 顯示擴展 name 的配置信息以交互式Shell模式運行PHP
http://php.net/manual/en/features.commandline.interactive.php
The interactive shell stores your history which can be accessed using the up and down keys. The history is saved in the ~/.php_history file.
交互shell模式保存輸入的歷史命令,可以使用上下鍵訪問到。歷史被保存在~/.php_history文件。
$ php -aInteractive shellphp echo 5+8;php function addTwo($n)php {php { return $n + 2;php { }php var_dump(addtwo(2));int(4)查找相關類、擴展或者函數的信息
通常,我們可以使用php --info命令或者在在web服務器上的php程序中使用函數phpinfo()顯示php的信息,然后再查找相關類、擴展或者函數的信息,這樣做實在是麻煩了一些。
$ php --info | grep redisredisRegistered save handlers = files user redisThis program is free software; you can redistribute it and/or modify語法檢查
只需要檢查php腳本是否存在語法錯誤,而不需要執行它,比如在一些編輯器或者IDE中檢查PHP文件是否存在語法錯誤。
使用-l(--syntax-check)可以只對PHP文件進行語法檢查。
$ php -l index.phpNo syntax errors detected in index.php
假如index.php中存在語法錯誤。
$ php -l index.phpPHP Parse error: syntax error, unexpected echo (T_ECHO) in index.php on line 3Parse error: syntax error, unexpected echo (T_ECHO) in index.php on line 3Errors parsing index.php命令行腳本
$argc 包含了 $argv數組包含元素的數目
$argv 是一個數組,包含了提供的參數,第一個參數總是腳本文件名稱
console.php的命令行腳本文件
?phpecho 命令行參數個數: . $argc . /n echo 命令行參數:/n foreach ($argv as $index = $arg) { echo {$index} : {$arg}/n
: hello: world
可以看到,第0個參數是我們執行的腳本名稱。需要注意的是,如果提供的第一個參數是以-開頭的話,需要在前面增加--,以告訴php這后面的參數是提供給我們的腳本的,而不是php執行文件的(php -r var_dump($argv); -- -h)。
另外,在腳本中,我們可以通過php_sapi_name()函數判斷是否是在命令行下運行的。
$ php -r echo php_sapi_name(), PHP_EOL; cli
以上就是PHP命令行(CLI模式)的詳細介紹的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答