请使用php自定义函数实现一个加减乘除的功能,函数名为operation,形式参数三个,第一个参数为$a,第二个参数为$b,第三个参数为符号(加减乘除符号)参数为$sign,例如使用operation(10,10,“*”)函数调用能够实现10*10的结果为100,这个100的结果必须用return返回,最后将结果用echo输出到页面。(注意:加减乘除都可以得出结果。
时间: 2024-03-15 21:47:49 浏览: 87
好的,下面是使用 PHP 自定义函数实现加减乘除功能的代码:
```php
<?php
function operation($a, $b, $sign)
{
$result = 0;
switch ($sign) {
case '+':
$result = $a + $b;
break;
case '-':
$result = $a - $b;
break;
case '*':
$result = $a * $b;
break;
case '/':
$result = $a / $b;
break;
default:
echo "非法符号!";
return;
}
return $result;
}
$a = 10;
$b = 10;
$sign = '*';
$result = operation($a, $b, $sign);
echo $a . $sign . $b . '=' . $result;
?>
```
这段代码中,我们定义了一个名为 `operation` 的自定义函数,这个函数有三个形式参数:$a、$b 和 $sign,分别代表两个操作数和操作符号。在函数内部,我们使用 `switch` 语句根据操作符号计算出结果,并将结果使用 `return` 返回。在主程序中,我们调用 `operation` 函数计算 $a 和 $b 的结果,并将结果输出到页面上。
阅读全文
相关推荐















