dog1888 发表于 2015-8-23 15:43:24

php cli命令行模式下的使用笔记

  php cli命令行模式是WIN下的一个SHELL,不需要APACHE的支持就能执行PHP脚本的脚本,并且是持续执行的。这些特点很容易利用来快速测试PHP脚本。今天就特意找来一些资料,整理了一下,权当复习。
  如果配置了环境变量,则在命令行下输入如下命令 php /?就可以看到如下帮助了。中文部分是我给出的一些注释
  Usage: php [-f] <file> [--]
       php -r <code> [--] 直接执行代码
       php [-B <begin_code>] -R <code> [-E <end_code>] [--]

  
       php [-B <begin_code>] -F <file> [-E <end_code>] [--]
  
       php --
       php -a
  -a               Run interactively交互运行
-c <path>|<file> Look for php.ini file in this directory在这个目录寻找php.ini文件
-n               No php.ini file will be used不使用php.ini文件
-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类似phpinfo()函数
-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               Display colour syntax highlighted source.高亮显示代码
-v               Version number版本信息
-w               Display source with stripped comments and whitespace.去除注释和空白后显示源代码
-z <file>      Load Zend extension <file>.加载ZEND扩展
  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>.显示扩展配置信息
  需要注意的是CLI模式和CGI模式运行时用的PHP.INI并非同一套配置,需要单独配置。
  <?php
if(!defined("STDIN")) {
define("STDIN", fopen('php://stdin','r'));
}
//php5.2里面STDIN已经定义了
echo "Hello! What is your name (enter below):\n";
$strName = fread(STDIN, 80);
echo 'Hello ',$strName, "\n";
?>
  >php --rf print_r#查看函数
  输出
Function [ <internal:standard> function print_r ] {
  
- Parameters {
    Parameter #0 [ <required> $var ]
    Parameter #1 [ <optional> $return ]
}
}
>php --re exif#查看扩展
  


exif扩展



1 Extension [ <persistent> extension #30 exif version 1.4 $Id: exif.c,v
2 1.173.2.5.
3 2.25 2008/03/12 17:33:14 iliaa Exp $ ] {
4   - Dependencies {
5   Dependency [ standard (Required) ]
6   Dependency [ mbstring (Required) ]
7   }
8   - INI {
9   Entry [ exif.encode_unicode <ALL> ]
10       Current = 'ISO-8859-15'
11   }
12   Entry [ exif.decode_unicode_motorola <ALL> ]
13       Current = 'UCS-2BE'
14   }
15   Entry [ exif.decode_unicode_intel <ALL> ]
16       Current = 'UCS-2LE'
17   }
18   Entry [ exif.encode_jis <ALL> ]
19       Current = ''
20   }
21   Entry [ exif.decode_jis_motorola <ALL> ]
22       Current = 'JIS'
23   }
24   Entry [ exif.decode_jis_intel <ALL> ]
25       Current = 'JIS'
26   }
27   }
28   - Constants {
29   Constant [ integer EXIF_USE_MBSTRING ] { 1 }
30   }
31   - Functions {
32   Function [ <internal:exif> function exif_read_data ] {
33       - Parameters {
34         Parameter #0 [ <required> $filename ]
35         Parameter #1 [ <optional> $sections_needed ]
36         Parameter #2 [ <optional> $sub_arrays ]
37         Parameter #3 [ <optional> $read_thumbnail ]
38       }
39   }
40   Function [ <internal:exif> function read_exif_data ] {
41       - Parameters {
42         Parameter #0 [ <required> $filename ]
43         Parameter #1 [ <optional> $sections_needed ]
44         Parameter #2 [ <optional> $sub_arrays ]
45         Parameter #3 [ <optional> $read_thumbnail ]
46       }
47   }
48   Function [ <internal:exif> function exif_tagname ] {
49       - Parameters {
50         Parameter #0 [ <required> $index ]
51       }
52   }
53   Function [ <internal:exif> function exif_thumbnail ] {
54       - Parameters {
55         Parameter #0 [ <required> $filename ]
56         Parameter #1 [ <optional> &$width ]
57         Parameter #2 [ <optional> &$height ]
58         Parameter #3 [ <optional> &$imagetype ]
59       }
60   }
61   Function [ <internal:exif> function exif_imagetype ] {
62       - Parameters {
63         Parameter #0 [ <required> $imagefile ]
64       }
65   }
66   }
67 }
68
  
  Dependency [ mbstring (Required) ]
  注意:在这里我们看到了exif对mbstring的依赖关系。也就解释了APPSERV启动时弹出找不到mbstring.DLL的疑惑了。
  参数读取
echo $_SERVER["argv"]."\n";
D:\AppServ\php5>php 1.php>1.txt 重定向
  




1 <?php
2$STDOUT = fopen('php://stdout', 'w');
3fwrite($STDOUT,"Hello World");
4fclose($STDOUT);
5?>



1 <?php
2set_time_limit(0);
3while(true){#持续运行
4 @fopen(time().".html","w");
5sleep(6);
6 }
7?>
  
我特意写了个BAT文件,方便拖拽执行PHP脚本
  
  




@ECHO OFF
echo 把要执行的文件拖拽到这里就可以了,注意路径的配置
set PHP_BIN=php.exe
%PHP_BIN% -f %1
pause
  
  
把这个保存为"解析PHP文件.bat",如果创建了环境变量的话,放在任何一个目录都是可以运行的。
页: [1]
查看完整版本: php cli命令行模式下的使用笔记