PHP的命令列指令碼開發,PHP命令列指令碼開發
PHP能做什麼
PHP官方文檔不要臉的說PHP能做任何事,這和業界廣為流傳氣死其他程式員不償命的PHP是最好的語言可真是遙呼相應。
PHP主要用於以下三個領域
(1) 服務端指令碼
這是最主要的領域,PHP 解析器(CGI 或者伺服器模組)和web伺服器(如Apache、Nginx)搭配使用。
(2) 命令列指令碼
這種方式,僅僅只需要 PHP 解析器來執行。聯想一下Python就會明白。
(3) 傳統型應用程式
通過一些擴充庫如PHP-GTK可以使用PHP編寫傳統型應用程式。不過這得多無聊才會去幹這事。
命令列開發
以下操作是在Mac下進行
進入php目錄,或將php目錄放到環境變數中。(Mac忽略這一步)
查看PHP引擎
php -v# 輸出PHP 5.5.27 (cli) (built: Jul 23 2015 00:21:59) Copyright (c) 1997-2015 The PHP GroupZend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
查看使用協助
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] php [options] -- [args...] php [options] -a -a Run as interactive shell -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 and execute <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 <addr>:<port> Run with built-in web server. -t <docroot> Specify document root <docroot> for built-in web server. -s Output HTML syntax highlighted source. -v Version number -w Output 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 stdin --ini Show configuration file names --rf <name> Show information about function <name>. --rc <name> Show information about class <name>. --re <name> Show information about extension <name>. --rz <name> Show information about Zend extension <name>. --ri <name> Show configuration for extension <name>.
執行一個PHP檔案
php [-f] xxx.php
可以傳參數
php [-f] xxx.php 'hello' 'world' 2015
傳遞給指令碼的參數可在全域變數$argv中擷取,全域變數$argc存有$argv數組中成員變數的個數(而非傳送給指令碼程式的參數的個數)
001.php
<?phpvar_dump($argc);echo "\n";var_dump($argv);?>
執行001.php
php 001.php 'hello world' 2015
輸出
int(3)array(3) { [0]=> string(7) "001.php" [1]=> string(11) "hello world" [2]=> string(4) "2015"}
也可以直接運行 PHP 代碼
php -r 'echo "Hello World\n";'#輸出Hello World
當然,PHP的命令列開發不僅以上這些內容,這裡可以查看更多http://php.net/manual/zh/features.commandline.php
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。