默认值_PHP
Jun 01, 2016 pm 12:39 PM
一个函数对于标量参数可以定义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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

iOS 17.2: How to change your iPhone's default notification sound

What are the uses of the Type keyword in Go?

Solve Ubuntu mounting mobile hard disk error: unknown file system type exfat

How to set the default download resolution on Youku

What should I do if my win10 system crashes and displays out of memory?

What are the requirements for setting the default MySQL account password?

How to change the default system startup music in win11

What information is required when registering a Deepseek account?
