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] [--] [args...] php [options] -r [--] [args...] php [options] [-B ] -R [-E ] [--] [args...] php [options] [-B ] -F [-E ] [--] [args...] php [options] -S : [-t docroot] php [options] -- [args...] php [options] -a -a Run as interactive shell -c | 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 Parse and execute . -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r Run PHP without using script tags -B Run PHP before processing input lines -R Run PHP for every input line -F Parse and execute for every input line -E Run PHP after processing all input lines -H Hide any passed arguments from external tools. -S : Run with built-in web server. -t Specify document root for built-in web server. -s Output HTML syntax highlighted source. -v Version number -w Output source with stripped comments and whitespace. -z Load Zend extension . args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin --ini Show configuration file names --rf Show information about function . --rc Show information about class . --re Show information about extension . --rz Show information about Zend extension . --ri Show configuration for extension .
執行一個PHP檔案
php [-f] xxx.php
可以傳參數
php [-f] xxx.php 'hello' 'world' 2015
傳遞給指令碼的參數可在全域變數$argv中擷取,全域變數$argc存有$argv數組中成員變數的個數(而非傳送給指令碼程式的參數的個數)
001.php
執行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;'#輸出Hello World
http://www.bkjia.com/PHPjc/1050840.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1050840.htmlTechArticlePHP的命令列指令碼開發 PHP能做什麼 PHP官方文檔不要臉的說 PHP能做任何事 ,這和業界廣為流傳氣死其他程式員不償命的 PHP是最好的語言 可真...