Jump to content

Default arguments


nomadrw

Recommended Posts

Hello everyone:

I new to this site and new to PHP.

I trying to learn PHP on my own and I don't understand this short example. I hoping someone can explain it to me.

Here is the code:

function tour_guild($city = "Gotham City",
                             $desc = "vast metropolis",
                             $how_many ="dozens",
                             $of_what = "costumed villains")
{
Print ("$city is a $desc filled with $how_many of $of_what. <br>");
}
tour_guild();
tour_guild("Chicago");
tour_guild("Chicago", wonderful city");
tour_guild("Chicago", "wonderful city",
                "teeming millions");
tour_guild("Chicago" "wonderful city",
                "gruff people with hearts of
                gold and hard_luck");

 

Not use how the lower parameter are printed (tour_guild) ie

tour_guild();

tour_guild("Chicago");

tour_guild("Chicago", wonderful city");

tour_guild("Chicago", "wonderful city",

                "teeming millions");

...

 

any help would be great.

 

 

 

 

Link to comment
Share on other sites

Values for parameters given in the function declaration designate default values if that specific parameter happens to not be passed, if it is passed it'll just be overlooked. Example:

 

function doSomething($var = 50)
{
     echo $var;
}

/*
Because I don't pass the first parameter in this example it assumes the default value designated in 
the function declaration. ($var = 50). As a result this function will echo '50'.
*/
doSomething();

/*
This time I do supply the first parameter in the function call so the default value of 50 is 
overlooked, and it just assumes the value that I supply in the function call. As a result this time the function echos 'Hello'.
*/
doSomething('Hello');

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.