Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:第一次听说是有点难, 多做几个练习就好了
//1.自定义函数
function userName():string
{
return '程东旭';
}
echo userName();
//2.系统函数
echo '<hr>';
function aaa():int
{
return time();
}
echo aaa();
//3.可变函数
echo '<hr>';
$type = 'email';
function email():string
{
return 'admin@php.cn';
}
echo $type();
//4.匿名函数,闭包
echo '<hr>';
$max = function (int $a, int $b):int
{
return max($a, $b);
};
echo $max(10, 20);
//1.返回值是字符串
echo '<hr>';
function user():string
{
$name = 'admin';
$email = 'admin@php.cn';
return $name . '的邮箱是' . $email;
}
echo user();
//2.返回值是数组
echo '<hr>';
function user1():array
{
return ['程东旭', 30, 'chengdongxu@php.cn'];
}
$user1 = print_r(user1(), true);
printf('<pre>%s</pre>', $user1);
//3.返回值是json
echo '<hr>';
function user2():string
{
return json_encode(['name' => 'chengdongxu', 'age' => 30, 'email' => 'chengdongxu@php.cn']);
}
$user2 = user2();
echo $user2;
//4.返回值是序列化
echo '<hr>';
function phone():string
{
return serialize(['name' => 'apple phone', 'price' => 8000, 'color' => 'red']);
}
$phone1 = phone();
echo $phone1;
echo '<hr>';
//反序列化
$phone2 = unserialize(phone());
printf('<pre>%s</pre>', print_r($phone2, true));
//1.值参数,仅传递值
$age2 = 30;
echo '<hr>';
function user4(int $age):int
{
return $age += 2;
}
echo '我现在的年龄是' . user4($age2) . '<br>';
echo '我2年前的年龄是' . $age2 . '<br>';
//2.引用传递
echo '<hr>';
function user5(int &$age2):int
{
return $age2 += 5;
}
$age3 = 30;
echo '我现在年龄是' . user5($age3) . '<br>';
echo '我现在年龄是' . $age3 . '<br>';
//3.默认参数
echo '<hr>';
function user6(int $a, int $b, string $opt = '*')
{
$res = 0;
switch ($opt) {
case '+':
$res = "$a + $b = " . ($a + $b);
break;
case '-':
$res = "$a - $b = " . ($a - $b);
break;
case '*':
$res = "$a * $b = " . ($a * $b);
break;
default:
$res = '非法操作';
}
return $res;
}
echo user6(300, 200);
echo user6(300, 200, '+');
//4.剩余参数
echo '<hr>';
function user7()
{
$num = 0;
foreach (func_get_args() as $value){
$num += $value;
}
return $num;
}
print_r(user7(1,5,55,66,33255,55,445,1));