2 回答
TA贡献1809条经验 获得超8个赞
function calculation($para1,$para2,$operator){
switch($operator){
case 'add':return $para1+$para2;
break;
case 'sub':return $para1-$para2;
break;
case 'mul':return $para1*$para2;
break;
case 'div':return $para1/$para2;
break;
default:return false;
}
}
function sum($start,$end,$step){
$sum=0;
for($i=$start;$i<=$end;$i+=$step){
$sum+=$i;
}
return $sum;
}
调用函数是一个返回值,如果你想函数直接输出,那么把return 改成 echo就行了
不过建议你这样调用:
echo calculation(3,4,'add'); // do addition
echo calculation(3,4,'sub'); // do subtraction
echo calculation(3,4,'mul'); // do multiplication
echo calculation(3,4,'div'); // do division
echo sum(1,100,1);
echo sum(1,100,2);
TA贡献1808条经验 获得超4个赞
function calculation($para1,$para2,$operator){
switch($operator){
case 'add':return $para1+$para2;
break;
case 'sub':return $para1-$para2;
break;
case 'mul':return $para1*$para2;
break;
case 'div':return $para1/$para2;
break;
default:return false;
}
}
function sum($start,$end,$step){
$sum=0;
for($i=$start;$i<=$end;$i+=$step){
$sum+=$i;
}
return $sum;
}
添加回答
举报