Default Value_PHP Tutorial

WBOY
Release: 2016-07-13 17:26:30
Original
842 people have browsed it

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


The output of this example is:
Making a bowl of acidophilus raspberry.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531965.htmlTechArticleA function can define C++-style default values ​​for scalar arguments. function makecoffee ($type = cappucino) { echo Making a cup of $type.; } echo makecoffee (); echo makecoffee (espr...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!