Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialElena Paraschiv
9,938 PointsWhat if $name=Null ?
The code in the video is
<?php
function get_info($name, $title = Null){
if($title){
echo "$name has arrived. They are with us as a $title.";
} else {
echo "$name has arrived. They have no title.";
}
}
get_info("Mike");
?>
What if instead we want the name to be null?
<?php
function get_info($name= Null, $title ){
if($name){
echo "$name has arrived. They are with us as a $title.";
} else {
echo "There is no $name. This is $title.";
}
}
get_info("frogs");
?>
What would we write in the parathesis? get_info();
I thought about this solution. Are there others?
<?php
function get_info($name= Null, $title="frogs" ){
if($name){
echo "$name has arrived. They are with us as $title.";
} else {
echo "$name has arrived. They have no $title.";
}
}
get_info();
?>
2 Answers
deckey
14,630 PointsHi there, not sure what you are trying out here...
As you probably checked by now, last example would just output:
has arrived. They have no frogs.
because $name was not specified and "frogs" were set as default values. What you set as parameter values when declaring a function is called a 'default' value. A value that a function will use if no other value for that argument was supplied.
Take a look at this:
function calc ($a=2, $b=2){
return $a * $b;
}
echo calc(); // returns 4
echo calc(3); // returns 6
echo calc(3,10); // returns 30
good luck!
Kyle Hunter
441 PointsI am having a similar question what if you wanted to echo has arrived. They have no dogs. I am not sure of an actually use case for this but just more of an outside the box thinking how do you pass just the second parameter? get_info('','dogs'); Would this be the correct use?