Does the optional parameter in PHP have to be followed by a required parameter?

Source: Internet
Author: User
Public Function Addhost ($groupId, $nodeId = "Node_0", $role = "Master", $hostConfig) {    //...}

It's always been a must in my image. The parameter is defined before the optional parameter, the above code is seen in the lotusphp source, how to define how to use it?

Reply content:

Public Function Addhost ($groupId, $nodeId = "Node_0", $role = "Master", $hostConfig) {    //...}

It's always been a must in my image. The parameter is defined before the optional parameter, the above code is seen in the lotusphp source, how to define how to use it?

function test ($a, $b = 3, $c = 4, $d) {echo $a. $b. $c. $d    ;} Echo Test (4, "", "", 9);//print result//49echo test (4, NULL, NULL, 9);//Print result//49

In fact $b = 3, the default value of $c = 4 will never be set successfully.
If the fourth parameter is a required parameter, then the function must have more than four arguments when it is used, at which point the default parameter is meaningless.

1. Your impression is not correct, the required parameters and optional parameters are not sequential, do not need to put optional parameters in the last
2. Lotusphp's Addhost () method is designed so that the main reason is that group, node, role is progressive at the conceptual level, and reverse order is logically detrimental to memory, and config
3. The calling method is Addhost ("Global_group",, Array ("host" = "127.0.0.1"), and more call methods see lotusphp/unittest/db/ rightwaytouse.php

Upstairs positive solution, PHP does not pay attention to the default parameters of the location, but the call can not be separated, such as
function test ($a, $b =3, $c =4, $d)
{
echo $a. $b. $c. $d;
}
echo Test (1,2,3,5);//Can be
echo Test (1,,, 5);//Error

  • 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.