Home > Article > Backend Development > How does the join function make the previous parameters omitted?
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:
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.
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:
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.