PHP 在命令行(cmd/shell)下传参的方式
侧边栏壁纸
  • 累计撰写 186 篇文章
  • 累计收到 24 条评论
php

PHP 在命令行(cmd/shell)下传参的方式

五好
2018-10-19 / 0 评论 / 891 阅读 / 正在检测是否收录...

1.预定义变量 $argv

这个变量 包含当运行于命令行下时传递给当前脚本的参数的数组。
数组的地一个元素 为脚本名称,后面的元素为参数
例如: 
$test.php a b 1
$argv[0]='test.php'
$argv[1]='a'
$argv[2]='b'
$argv[3]=1
而$argc — 传递给脚本的参数数目

2.函数getopt array getopt ( string $options [, array $longopts [, int &$optind ]] )

options
该字符串中的每个字符会被当做选项字符,匹配传入脚本的选项以单个连字符(-)开头。 比如,一个选项字符串 "x" 识别了一个选项 -x。 只允许 a-z、A-Z 和 0-9。
longopts
选项数组。此数组中的每个元素会被作为选项字符串,匹配了以两个连字符(--)传入到脚本的选项。 例如,长选项元素 "opt" 识别了一个选项 --opt。
optind
If the optind parameter is present, then the index where argument parsing stopped will be written to this variable.
options 可能包含了以下元素:

单独的字符(不接受值)
后面跟随冒号的字符(此选项需要值)
后面跟随两个冒号的字符(此选项的值可选)
更多知识参考 http://php.net/manual/zh/function.getopt.php
0

评论 (0)

取消