nomadrw Posted November 12, 2009 Share Posted November 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181320-default-arguments/ Share on other sites More sharing options...
Alex Posted November 12, 2009 Share Posted November 12, 2009 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'); Quote Link to comment https://forums.phpfreaks.com/topic/181320-default-arguments/#findComment-956498 Share on other sites More sharing options...
nomadrw Posted November 12, 2009 Author Share Posted November 12, 2009 Thanks AlexWD. I get it now. nomad Quote Link to comment https://forums.phpfreaks.com/topic/181320-default-arguments/#findComment-956519 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.