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
https://forums.phpfreaks.com/topic/181320-default-arguments/
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
https://forums.phpfreaks.com/topic/181320-default-arguments/#findComment-956498
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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