How would I skip optional arguments in a function

2019-01-02 18:43发布

OK I totally forgot how to skip arguments in PHP.

Lets say I have:

function getData($name, $limit = '50', $page = '1') {
    ...
}

How would I call this function so that the middle parameter takes the default value (ie. '50')?

getData('some name', '', '23');

Would the above be correct? I can't seem to get this to work.

17条回答
刘海飞了
2楼-- · 2019-01-02 19:02

Your post is correct.

Unfortunately, if you need to use an optional parameter at the very end of the parameter list, you have to specify everything up until that last parameter. Generally if you want to mix-and-match, you give them default values of '' or null, and don't use them inside the function if they are that default value.

查看更多
人间绝色
3楼-- · 2019-01-02 19:03

Nope, it's not possible to skip arguments this way. You can omit passing arguments only if they are at the end of the parameter list.

There was an official proposal for this: https://wiki.php.net/rfc/skipparams, which got declined. The proposal page links to other SO questions on this topic.

查看更多
春风洒进眼中
4楼-- · 2019-01-02 19:04

This snippet:


    function getData($name, $options) {
       $default = array(
            'limit' => 50,
            'page' => 2,
        );
        $args = array_merge($default, $options);
        print_r($args);
    }

    getData('foo', array());
    getData('foo', array('limit'=>2));
    getData('foo', array('limit'=>10, 'page'=>10));

Answer is :


     Array
    (
        [limit] => 50
        [page] => 2
    )
    Array
    (
        [limit] => 2
        [page] => 2
    )
    Array
    (
        [limit] => 10
        [page] => 10
    )

查看更多
宁负流年不负卿
5楼-- · 2019-01-02 19:04

Try This.

function getData($name, $limit = NULL, $page = '1') {
               if (!$limit){
                 $limit = 50;
               }
}

getData('some name', '', '23');
查看更多
倾城一夜雪
6楼-- · 2019-01-02 19:06

This is what I would do:

<?php

    function getData($name, $limit = '', $page = '1') {
            $limit = (EMPTY($limit)) ? 50 : $limit;
            $output = "name=$name&limit=$limit&page=$page";
            return $output;
    }

     echo getData('table');

    /* output name=table&limit=50&page=1 */

     echo getData('table',20);

    /* name=table&limit=20&page=1 */

    echo getData('table','',5);

    /* output name=table&limit=50&page=5 */

    function getData2($name, $limit = NULL, $page = '1') {
            $limit = (ISSET($limit)) ? $limit : 50;
            $output = "name=$name&limit=$limit&page=$page";
            return $output;
    }

    echo getData2('table');

    // /* output name=table&limit=50&page=1 */

    echo getData2('table',20);

    /* output name=table&limit=20&page=1 */

    echo getData2('table',NULL,3);

    /* output name=table&limit=50&page=3 */

?>

Hope this will help someone

查看更多
骚的不知所云
7楼-- · 2019-01-02 19:08

For any parameter skipped (you have to) go with the default parameter, to be on the safe side.

(Settling for null where the default parameter is '' or similar or vice versa will get you into troublew...)

查看更多
登录 后发表回答