shell执行php传参

shell执行PHP时,传参的三种方式

亡梦爱人 提交于 2020-04-09 20:06:40
php执行http请求时,可以使用GET或POST请求,但在shell命令行下无法使用。 以下为命令行下的三种传值方式 一、使用 $argv & $ $argc 参数接收 $argc :传递到脚本的参数数目 Manual $argv :传递给脚本的参数数组 Manual 注意: 这两个变量仅在register_argc_argv打开时可用 第一个参数总是当前脚本的文件名 php代码 <?php var_dump($argc); var_dump($argv); shell命令 //sh命令 php index.php a b c //结果 Array( [0] => index.php [1] => a [2] => b [3] => c ) 二、使用getopt函数 getopt() : 从命令行参数列表中获取选项 // 使用格式 array getopt ( string $options [, array $longopts [, int &$optind ]] ) //参数 1.options : 该字符串中的每个字符会被当做选项字符,匹配传入脚本的选项以单个连字符(-)开头。 比如,一个选项字符串 "x" 识别了一个选项 -x。 只允许 a-z、A-Z 和 0-9。 1.1单独的字符(不接受值) 1.2后面跟随冒号的字符(此选项需要值) 1.3后面跟随两个冒号的字符