A function can define C++-style default values for scalar arguments.
function makecoffee ($type = "cappucino") {
echo "Making a cup of $type.
";
}
echo makecoffee ();
echo makecoffee ("espresso");
The output of the above program segment is as follows:
Making a cup of cappucino.
Making a cup of espresso.
The default value must be a constant expression, not a variable or class member.
Note that when using default parameters, any defaults should be to the right of any non-default parameters; otherwise, things will not Think of it like you want. Consider the following program segment:
function makeyogurt ($type = "acidophilus", $flavour) {
return "Making a bowl of $type $flavour.
";
}
echo makeyogurt ("raspberry"); // Will not work as expected
The output of the above example is:
Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .
Now, compare the following with the above:
function makeyogurt ($flavour , $type = "acidophilus"){
return "Making a bowl of $type $flavour.
";
}
echo makeyogurt ("raspberry");//Works normally