How can a join function omit the preceding parameters?

Source: Internet
Author: User
To encapsulate a function with a function similar to the join (implode alias), the code is as follows: {code ...} in this way, the join operation is similar to that of the system function. However, if the $ glue parameter is not specified during the call, an error is returned. I know that parameters with default values should be placed behind them, such as getJoin ($ arr, $ gl...

To encapsulate a function with a function similar to the join (implode alias), the code is as follows:


  $ Value) {$ str. = $ value. $ glue;} $ str = substr ($ str, 0,-1); return $ str;} echo getJoin (",", $ arr );

In this way, the join operation is similar to that of the system function. However, if the $ glue parameter is not specified during the call, an error is returned.

I know that parameters with default values should be placed behind them, for examplegetJoin($arr,$glue="")In this way, only the $ arr parameter can be filled in. However, after reading the manual, the system function join parameter value is as follows:

No brackets indicate that the parameter cannot be omitted. at the same time, it also supports writing only one parameter and uses an empty string as the $ glue value by default. Although there are some historical reasons, I still want to know how the system functions are implemented.

Reply content:

To encapsulate a function with a function similar to the join (implode alias), the code is as follows:


  $ Value) {$ str. = $ value. $ glue;} $ str = substr ($ str, 0,-1); return $ str;} echo getJoin (",", $ arr );

In this way, the join operation is similar to that of the system function. However, if the $ glue parameter is not specified during the call, an error is returned.

I know that parameters with default values should be placed behind them, for examplegetJoin($arr,$glue="")In this way, only the $ arr parameter can be filled in. However, after reading the manual, the system function join parameter value is as follows:

No brackets indicate that the parameter cannot be omitted. at the same time, it also supports writing only one parameter and uses an empty string as the $ glue value by default. Although there are some historical reasons, I still want to know how the system functions are implemented.

Function overload...
Http://m.blog.csdn.net/articl...

If you must achieve this, you can use the php variable parameter quantity declaration method. See
Http://php.net/manual/zh/func...
Get the parameter to the array and process it by analyzing the array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.