Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
参数:可选的,对外提供一个接口,供函数调用者按照自己的意愿改变函数体内的执行行为
参数 形参 实参
默认参数:有默认值的参数,如果不传参或者少传参,就会默认参数的值
参数是从左往右求值,所以默认参数在最右边
按值传递参数 不会改变全局变量的值 导入到函数中的只是$roomprice的副本
function totalneedtopay1($days, $roomprice, $discount = 0.88)
{
$roomprice *= $discount;
$total = $days * $roomprice;
return "您需要支付的总价为{$total}元。";
}
$days = 5;
$roomprice = 1500;
$discount = 0.7;
echo totalneedtopay1($days, $roomprice, $discount);
按变量引用传值 会改变父作用域里变量的值 变量内容所处的内存地址会被导入的函数中
function totalneedtopay2($days, &$roomprice, $discount = 0.88)
{
$roomprice *= $discount;
$total = $days * $roomprice;
return "您需要支付的总价为{$total}元。";
}
$days = 5;
$roomprice = 1500;
$discount = 0.7;
echo totalneedtopay2($days, $roomprice, $discount);
echo $roomprice;
return 返回值
function demo()
{
return md5('123456');
return 1 === '1';
return 1 == '1';
return array('123', '西门子');
return 12.23;
echo '你好';
return 1;
}
$res = demo();
var_dump($res);
$closure = function ($name) {
return "{$name},欢迎您下榻喜来登酒店。";
};
echo $closure('李女士');
$odd = function (array $arr) {
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] % 2 == 0) {
$newArr[] = $arr[$i];
}
}
return $newArr;
};
$arr = [24, 36, 55, 87, 96, 24, 65, 66];
var_dump($odd($arr));
function sum(closure $func, $arr)
{
return array_sum($func($arr));
}
echo sum($odd, $arr);