默认值_PHP教程

WBOY
Freigeben: 2016-07-13 17:26:30
Original
770 Leute haben es durchsucht

一个函数对于标量参数可以定义C++-风格的默认值.
function makecoffee ($type = "cappucino") {
echo "Making a cup of $type. ";
}
echo makecoffee ();
echo makecoffee ("espresso");
上面的程序段的输出如下:
Making a cup of cappucino.
Making a cup of espresso.
默认值必须是一个常量表达式,不是一个变量或类成员.
注意当时用默认参数时,任何默认都应该在任何非默认参数右边;否则,事情将不会想你所想的那样.考虑下面的程序段:
function makeyogurt ($type = "acidophilus", $flavour) {
return "Making a bowl of $type $flavour. ";
}
echo makeyogurt ("raspberry"); // 将不会按照预想工作
上面例子的输出是:
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 .
现在,用下面的对比上面的:
function makeyogurt ($flavour, $type = "acidophilus"){
return "Making a bowl of $type $flavour. ";
}
echo makeyogurt (“raspberry”);//正常工作


这个例子的输出是:
Making a bowl of acidophilus raspberry.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/531965.htmlTechArticle一个函数对于标量参数可以定义C++-风格的默认值. function makecoffee ($type = cappucino) { echo Making a cup of $type.; } echo makecoffee (); echo makecoffee (espr...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!