Correcting teacher:Guanhui
Correction status:qualified
Teacher's comments:代码写的可以!整体还需加点文字描述下!
函数名:只允许字母数字或下划线组成,且不能用数字开头
function isOdd(int $num):bool{
if ($num % 2 == 0):
return false;
else :
return true;
endif;
}
echo '<br>';
echo isOdd(5) ? '是奇数' : '是偶数';
function isEven(int $num):bool{
if ($num % 2 == 0):
return true;
else :
return false;
endif;
}
echo '<br>';
echo isEven(5) ? '是偶数' : '是奇数';
//判断变量是不是整数类型
$a = '12';
echo is_int($a): '是整数类型':'不是整数类型';
function isEven(int $num){
if($num % 2 == 1):
echo '奇数';
else:
echo '偶数';
endif;
}
isEven(8);
$middle = "isEven";
echo '<br>';
$middle(9);
$sum = function(int $a,int $b){
echo '<br>',"$a + $b =" .($a+$b);
};
$sum(11,34);
$per = 0.5;
$result = function (float $money, int $num) use ($per) : float
{
$total = $money * $num;
return ($total > 20000) ? ($total* $per) : $total;
};
echo '<hr>','实付金额 = ' . $result(20000, 4), '<hr>';
//用匿名函数做闭包,可以保证变量安全性
$a = function(){
$c = 1;
$d = function(int $i, int $j) use ($c) :int{
return ($i + $j);
};
return $d;
};
echo '<hr>'.$a()(2,4),'<hr>';
function demo1():string{
$status = 200;
$message = '返回成功';
return $status.':'.$message;
}
echo demo1();
function demo2():array{
$status = 200;
$message = '返回成功';
return ['status'=>$status,'msg'=>$message];
}
echo demo2()['status'] === 200 ? demo2()['msg'] : '未获取到数据','<hr>';
function demo3():string{
$status = 200;
$message = '返回成功';
return json_encode(['status'=>$status,'msg'=>$message]);
}
echo demo3(),'<hr>';
function demo4():string{
$status = 200;
$message = '返回成功';
return serialize(['status'=>$status,'msg'=>$message]);
}
echo demo4(),'<hr>';
//返回
//a:2:{s:6:"status";i:200;s:3:"msg";s:12:"返回成功";}
// 反序列化: 还原
print_r(unserialize(demo4())['msg']);
function demo1(int $num){
return $num += 3;
}
echo demo1(11),'<hr>';
function demo6(int &$num){
$num = 13;
}
$num = 18;
echo '初始值:',$num,'<hr>'; // 初始值:18
demo6($num);
echo $num,'<hr>';//13
function demo7(int $age,string $sex = '男'){
return '年龄:'.$age .'<br>性别:'.$sex.'<br>';
}
echo demo7(13);
function demo8(...$args){
return max($args);
}
$arr = [34,21,12,112,99];
//数组变成多个值传入函数 可以用...$arr
$max = demo8(...$arr);
print_r($max );
namespace space1{
function demo1(){
return "这是space1";
}
}
namespace space2{
function demo1(){
return "这是space2";
}
}
namespace {
echo space1\demo1();
}