欢迎光临
我们一直在努力

默认值-PHP教程,其它文章

建站超值云服务器,限时71元/月

一个函数对于标量参数可以定义c++-风格的默认值.

function makecoffee ($type = "cappucino") {

echo "making a cup of $type.\n";

}

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.\n";

}

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.\n";

}

echo makeyogurt (“raspberry”);//正常工作

这个例子的输出是:

making a bowl of acidophilus raspberry.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 默认值-PHP教程,其它文章
分享到: 更多 (0)

相关推荐

  • 暂无文章