1、值传递
$pricetag = 15.00;
$salestax = .075;
calcSalesTax(15.00,.075);
calcSalesTax($pricetag,$salestax)
2、引用传递
希望在函数内对参数的修改也可以体现在函数的作用域外,加&
$cost = 20.99;
$tax =0.0575;
function calculateCost(&$cost,$tax)
{
$cost = $cost+($cost * $tax);
$tax += 4;
}
calculateCost($cost,$tax);
printf("Tax is %0.1.2f%%",$tax*100);
printf("Cost is : $%01.2f",$cost);
//结果$tax相同,$cost已经改变
3、默认参数值
如果有两个默认值,暂发现无法设置函数第二参数使用默认值,第三参数手动传入的方法
function calcSalesTax($price,$tax=.0675,$tax2=2.5)
{
echo "price=$price, tax=$tax, tax2=$tax2";
}
4、可选参数值
放在参数列表末尾,而且要指定其默认值为空
function calcSalesTax($price,$tax="",$tax2="")
{
echo "price=$price, tax=$tax, tax2=$tax2";
}
5、类型提示
强制参数为某个类的对象或数组
function processPayPalPayment(Customer $customer)
{
}
6、多个返回值
function retrieveUserProfile()
{
$user[] = "Jason Gilmore";
$user[] = "Jasin@example.com";
$user[] = "English";
return $user;
}
list($name,$email,$language)=retrieveUserProfile();
echo "\nName: $name, email:$email,language:$language\n";
7、函数库
require_once("taxation.library.php");