企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### CLI模式 cli是php的命令行运行模式,例如:我们在linux下经常使用 `"php -m"`查找PHP安装了那些扩展就是PHP命令行运行模式;有兴趣的同学可以输入php -h去深入研究该运行模式 ~~~ C:\Users\amamatthew>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 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 <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>. ~~~ 1. 让 PHP 运行指定文件。 ~~~ php script.php php -f script.php ~~~ 以上两种方法(使用或不使用 -f 参数)都能够运行脚本的script.php。可以选择任何文件来运行,您指定的 PHP 脚本并非必须要以 .php 为扩展名,它们可以有任意的文件名和扩展名。 2. 在命令行直接运行 PHP 代码。 ~~~ php -r "print_r(get_defined_constants());" ~~~ 在使用这种方法时,请您注意外壳变量的替代及引号的使用。 注: 请仔细阅读以上范例,在运行代码时没有开始和结束的标记符!加上 `-r` 参数后,这些标记符是不需要的,加上它们会导致语法错误。 3. 通过标准输入(stdin)提供需要运行的 PHP 代码。 以上用法给我们提供了非常强大的功能,使得我们可以如下范例所示,动态地生成 PHP 代码并通过命令行运行这些代码: ~~~ $ some_application | some_filter | php | sort -u >final_output.txt ~~~