What is the good example of using 'func_get_arg' in PHP?

后端 未结 7 1157
梦谈多话
梦谈多话 2021-02-07 00:39

I just have found out that there is a function called func_get_arg in PHP which enables developer to use variant style of getting arguments. It seems to be very us

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 01:19

    I hardly ever use func_get_arg(), but I do use its cousin func_get_args() quite a bit. Here's one example, a function along the lines of the echo statement that entity encodes all its arguments:

    function ee() {
        $args = func_get_args();
        echo implode('', array_map('htmlentities', $args));
    }
    

    I use that function quite a bit.

    Here's another useful example, a function that does the same job as SQL's COALESCE() function.

    function coalesce() {
        $args = func_get_args();
        foreach ($args as $arg) {
            if (!is_null($arg)) {
                return $arg;
            }
        }
        return null;
    }
    

    It returns the first non-null argument passed in, or null if there's no such argument.

提交回复
热议问题