什么是PHP-CLI
php-cli是php Command Line Interface的简称,即PHP命令行接口,在windows和linux下都是支持PHP-CLI模式的;
为什么要使用PHP-CLI
多线程应用、定时执行php程序、开发桌面程序 (使用PHP-CLI和GTK包即可开发桌面,但没人会用PHP来编写桌面程序的)、编写PHP的shell脚本
判断PHP运行模式
PHP的运行模式远远不止apache和cli,还包括:olserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.
echo php_sapi_name(); //如果是CLI模式下访问就输出CLI,如果是Apache就是apache2handler...
PHP-CLI 内置参数
D:\wamp\bin\php\php5.3.8>php -help
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 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 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>. --ri <name> Show configuration for extension <name>.
运行指定的php文件:
<?php echo 'this is php-cli' ?>
# php /var/www/html/test.php
this is a php-cli
[root@semple html]# php -f 'test.php'
this is a php-cli
在命令行直接运行 PHP 代码
D:\wamp\bin\php\php5.3.8>php -r "echo 'hello world';"
hello world
注意: 在运行这些php代码时没有开始和结束的标记符!加上 -r 参数后,这些标记符是不需要的,加上它们会导致语法错误。
通过标准输入(stdin)提供需要运行的 PHP 代码
// ask for input fwrite(STDOUT, "Enter your name: "); // get input $name = trim(fgets(STDIN)); // write input back fwrite(STDOUT, "Hello, $name!");
D:\wamp\www>php test.php
Enter your name:
D:\wamp\www>php test.php
Enter your name: zhouzhou Hello, zhouzhou!
获取自定义参数
print_r($argv); //获取具体的参数; print_r($argc); //获取参数的数目;
D:\wamp\www>php test.php #本身执行的php文件就作为一个参数;
Array ( [0] => test.php )
D:\wamp\www>php test.php arg1 arg2 arg3 arg4
Array ( [0] => test.php [1] => arg1 [2] => arg2 [3] => arg3 [4] => arg4 )
argv和argc也分别可以在$_SERVER数组中得到
<?php $args = getopt('g:m:a:'); //只能是单个单词,如果不是单个单词就会出错; print_r($args); ?>
D:\wamp\www>php test.php -g group -m module -a age
Array ( [g] => group [m] => module [a] => age )