命令行向Php传递参数的两种方式
2016-01-24 23:37
896 查看
第一种:getopt()
php中的getopt是用来接收参数时用的,用法举例:paramtest.php<?php $options = getopt('a:b:c:'); var_dump($options)
命令行测试:
bash-3.2# php paramtest.php -a 1 -b 2 -c 3 array(3) { 'a' => string(1) "1" 'b' => string(1) "2" 'c' => string(1) "3" } bash-3.2# php paramtest.php -a 1 -b 2 -c 3 -d 4 array(3) { 'a' => string(1) "1" 'b' => string(1) "2" 'c' => string(1) "3" }
当在getopt中注册的参数不存在时,php程序是不会接收的。
第二种:argvorargc参数接收
argv是接收的参数数组,argc是接收的参数的个数,举例:bash-3.2# php paramtest.php -a 1 -b 2 -c 3 -d 4 参数的个数:9 Array ( [0] => paramtest.php [1] => -a [2] => 1 [3] => -b [4] => 2 [5] => -c [6] => 3 [7] => -d [8] => 4 )
注:php执行的文件会默认成为argv的第一个参数
俩变量仅在 register_argc_argv 打开时可用
第三种:提示用户输入
程序如下:<?php fwrite(STDOUT, '请输入信息:'); echo '您输入的信息是:'.fgets(STDIN);
测试:
bash-3.2# php paramtest.php 请输入信息:hello world 您输入的信息是:hello world bash-3.2#
相关文章推荐
- Php中heredoc与nowdoc的使用方法
- PHP——数组2(数组函数,二维数组,正则表达式)
- PHP-文件上传到独立服务器
- php面向对象基础部分
- vsftp,samba,nfs区别和配置
- PHP send email configuration in XAMPP
- 93. php 命名空间(3)
- openwrt 安装 php sqlite
- 如何解决PHP里大量数据循环时内存耗尽的问题
- PHP——简单的表单提交
- IIS下PHP运行环境搭建与网页服务器故障排查方法
- PHP——数组中的each(),list()和while循环遍历数组
- php 2015 年最热门的 10 篇技术文章
- 快速排序的php实现
- Thinkphp源码分析-命名空间自动加载
- PHP 魔术方法
- 【转】WordPress转PHPCMS策略-数据库完美转换
- php课程---数组建立表单
- 学习php的socket
- ntpdate[16603]: the NTP socket is in use