zoukankan      html  css  js  c++  java
  • 【Thinkphp】 CLI模式下接收参数的几种方式

    PHP 中 CLI模式下接收参数的三大方式

    1 argv 方式

    php test.php news 1 5
    
    //变量仅在 register_argc_argv 打开时可用。
    print_r($argc); //CLI下获取参数的数目,最小值为1
    print_r($argv); //CLI下传递给脚本的参数数组,第一个参数总是当前脚本的文件名,因此 $argv[0] 就是脚本文件名
    //结果
    Array
    (
        [0] => test.php
        [1] => news
        [2] => 1
        [3] => 5
    )
    

    2 getopt 方式

    php test.php -a 1 -b 2 -c 3 -d=4 -e 5
    
    $param = getopt('a:b:c:d::e');
    print_r($param);
    
    //结果
    
    Array
    (
        [a] => 1
        [b] => 2
        [c] => 3
        [d] => 4
        [e] => 
    )
    
    

    3 longopts 方式

    php test.php --type news --is_hot 1 --limit=10 --expire=100
    
    $longopt = array(
        'type:',
        'is_hot:',
        'limit::',
        'expire'
    );
    $param = getopt('', $longopt);
    print_r($param);
    //结果
    Array
    (
        [type] => news
        [is_hot] => 1
        [limit] => 10
        [expire] => 
    )
    

    注意这种

    找到第一非选项,后面忽略实例

    php test.php --type news --is_hots 1 --limit=10 --expire=100
    
    $longopt = array(
        'type:',
        'is_hot:',
        'limit::',
        'expire'
    );
    $param = getopt('', $longopt);
    print_r($param);
    //结果
    Array
    (
        [type] => news
    )
    //因为is_hots不是选项值(定义的是is_hot),所以从这里开始之后的参数,都被丢弃
    

    在thinkphp 中, 传参 可以用 这种

    php  www/public/index.php  index/Test/run/page/111/page2/222/page3/333
    等效于
    www.test.com/index/Test/run?page=111&page2=222&page3=333
    

    此处的 page 就相当于 get 方式请求的参数

  • 相关阅读:
    mysql系列二、mysql内部执行过程
    mysql系列一、mysql数据库规范
    Centos6.5使用yum安装mysql——快速上手必备
    linux安装tomcat
    linux安装jdk
    tar 解压缩命令
    java并发编程系列四、AQS-AbstractQueuedSynchronizer
    JS数组方法汇总 array数组元素的添加和删除
    如何提升工作效率
    Excel学习笔记
  • 原文地址:https://www.cnblogs.com/richerdyoung/p/14236331.html
Copyright © 2011-2022 走看看