1. 程式人生 > >PHP命令列(CLI模式)

PHP命令列(CLI模式)

CLI模式

CLI模式其實就是命令列執行模式,英文全稱Command-Line Interface(命令列介面)

$ php -h
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] -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 -a
Interactive shell

php > echo 5+8;
13
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 redis
redis
Registered save handlers => files user redis
This program is free software; you can redistribute it and/or modify

語法檢查

只需要檢查php指令碼是否存在語法錯誤,而不需要執行它,比如在一些編輯器或者IDE中檢查PHP檔案是否存在語法錯誤。

使用-l(--syntax-check)可以只對PHP檔案進行語法檢查。

$ php -l index.php
No syntax errors detected in index.php

假如index.php中存在語法錯誤。

$ php -l index.php
PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3
Parse error: syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3
Errors parsing index.php

命令列指令碼

$argc 包含了 $argv陣列包含元素的數目
$argv 是一個數組,包含了提供的引數,第一個引數總是指令碼檔名稱
console.php的命令列指令碼檔案

<?php
echo '命令列引數個數: ' . $argc . "\n";
echo "命令列引數:\n";
foreach ($argv as $index => $arg) {
    echo "    {$index} : {$arg}\n";
}

$ php console.php hello world
命令列引數個數: 3
命令列引數:
    0 : console.php
    1 : hello
    2 : world

可以看到,第0個引數是我們執行的指令碼名稱。需要注意的是,如果提供的第一個引數是以-開頭的話,需要在前面增加--,以告訴php這後面的引數是提供給我們的指令碼的,而不是php執行檔案的(php -r 'var_dump($argv);' -- -h)。
另外,在指令碼中,我們可以通過php_sapi_name()函式判斷是否是在命令列下執行的。

$ php -r 'echo php_sapi_name(), PHP_EOL;'
cli