Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:完成的相当不错
function fun1($a,$b){
//显式
return $a+$b;
//隐式:结束大括号时候自动返回
}
echo fun1 (10,20);
function getPrice($a,$b){
$sum=$a+$b;
}
getPrice (1,2);
$str='今天上课真开心';
echo mb_substr ($str,0,5);
$fileName='hhh.png';
$action= trim (strrchr ($fileName,'.'),'.');
function jpg(){
echo 'jpg';
}
function png(){
echo 'png';
}
echo $action($fileName);
$discount=0.8;
$getAmount =function (float $money, int $num) use($discount):float
{
$amount =$money*$num;
return $amount>=2000?$amount*$discount:$amount;
};
//echo $getAmount(100,20);
$discount=0.8;
$f=function ($discount){
$getAmount =function (float $money, int $num) use($discount):float
{
$amount =$money*$num;
return $amount>=2000?$amount*$discount:$amount;
};
return $getAmount;
};
//echo $f(0.6)(200,20);
function demo1(){
$status=1;
$message='成功';
return $status . $message;
}
echo demo1 ();
function demo2(){
return ['status'=>'1' , 'message'=>'成功'];
}
//echo print_r (demo2(),true);
function demo3(){
//json_encode ();将php数据编码为json返回
return json_encode (['status'=>'1' , 'message'=>'成功']);
}
$data= demo3 ();
//将json格式的字符串还原/解析成php的数组或者对象 第二个参数要传true
$var =json_decode ($data,true);
//print_r ($var);
//这是返回值
{
"status":"1",
"message":"\u6210\u529f"
}
function demo4(){
return serialize (['status'=>'1' , 'message'=>'成功']);
}
echo demo4 ();
//a:2:{s:6:"status";s:1:"1";s:7:"message";s:6:"成功";} 这是值
print_r (unserialize (demo4 ()));
//这是返回值
//Array ( [status] => 1 [message] => 成功 )
function demo1($arg){
return $arg *=2;
}
$val=100;
echo demo1 ($val),'<br>';
//在函数中改变了调用参数的值 ,但是原始调用参数并没有发生变化
echo $val .'<hr>';
function demo1(&$arg){
return $arg *=2;
}
$val=100;
echo demo1 ($val),'<br>';
//在函数中改变了调用参数的值 ,但是原始调用参数并没有发生变化
echo $val .'<hr>';
function demo3(float $a,float $b,string $opt='+')
{
if ($opt=='+'){
$res=$a+$b;
} elseif ($opt=='-'){
$res=$a-$b;
}elseif ($opt=='*'){
$res=$a*$b;
}elseif ($opt=='/'){
$res=$a/$b;
}else{
$res='去你的吧';
}
return $res;
}
//echo demo3 (10,20).'<br>';
echo demo3 (10,20,'1').'<br>';
//echo '<hr>';
//剩余参数归纳符
function demo6(...$args)
{
return $args;
}
//print_r (demo6(1,2,3,4,5,6,8)) ;
//echo 123;
function demo6(...$args)
{
return array_sum ($args);
}
//print_r (demo6(1,2,3,4,5,6,8)) ;
//echo 123;
$args=[1,23,5,123,51];
//1.用在函数的形式参数列表中,表示收集,将多个离散的参数打包到一个数组中
//2.用在函数的调用列表中,表示展开,还原将一个数组展开成一个个参数值
$arr[]=[101,'php1','html1'];
$arr[]=[102,'php2','html2'];
$arr[]=[103,'php3','html3'];
//数组解包
foreach ($arr as list($id ,$name ,$grade)){
printf ('id=%s,name=%s<br>',$id,$name);
}