Home  >  Article  >  Backend Development  >  How does the join function make the previous parameters omitted?

How does the join function make the previous parameters omitted?

WBOY
WBOYOriginal
2016-10-10 11:56:111292browse

I want to encapsulate a function similar to join (alias of implode), the code is as follows:

<code><?php
#封装函数:join
$arr = array('jack','male',23);
function getJoin($glue="",$arr){
    $str ="";
    foreach ($arr as $key => $value) {
        $str .= $value.$glue;
    }
    $str = substr($str, 0,-1);
    return $str;
}
echo getJoin(",",$arr);</code>

Written like this, it looks similar to the system function join, but if the $glue parameter is not filled in when calling, an error will be reported.

I know that parameters with default values ​​should be placed at the end, such as getJoin($arr,$glue=""). In this way, only the $arr parameter can be filled in when calling, but I read the manual and found the parameters of the system function join The value is like this:

How does the join function make the previous parameters omitted?

The absence of square brackets indicates that the parameter cannot be omitted, and at the same time, it also supports the writing method of filling in only one parameter and uses the empty string as the value of $glue by default. Although the explanation is for historical reasons, I still want to know how the system function is implemented.

Reply content:

I want to encapsulate a function similar to join (alias of implode), the code is as follows:

<code><?php
#封装函数:join
$arr = array('jack','male',23);
function getJoin($glue="",$arr){
    $str ="";
    foreach ($arr as $key => $value) {
        $str .= $value.$glue;
    }
    $str = substr($str, 0,-1);
    return $str;
}
echo getJoin(",",$arr);</code>

Written like this, it looks similar to the system function join, but if the $glue parameter is not filled in when calling, an error will be reported.

I know that parameters with default values ​​should be placed at the end, such as getJoin($arr,$glue=""). In this way, only the $arr parameter can be filled in when calling, but I read the manual and found the parameters of the system function join The value is like this:

How does the join function make the previous parameters omitted?

The absence of square brackets indicates that the parameter cannot be omitted, and at the same time, it also supports the writing method of filling in only one parameter and uses the empty string as the value of $glue by default. Although the explanation is for historical reasons, I still want to know how the system function is implemented.

Function overloading. . .
http://m.blog.csdn.net/articl...

If you must achieve this effect, you can use PHP’s variable number of parameter declaration method. See
http://php.net/manual/zh/func...
Get the parameters into the array and process them by analyzing the array.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn