function 函數名(形參列表) {
函數體;
return 語句;
}
例:實現一個簡單的加、減、乘、除計算器
test.php:
<?php
require 'funcs.php';
$num1 = 56;
$num2 = 44;
$oper = "+";
$res = jisuan($num1, $num2, $oper); //調用自定義函數
echo "計算結果是:" . $res;
?>
funcs.php:
<?php
function jisuan($num1, $num2, $oper) {
switch ($oper) {
case "+":
$res = $num1 + $num2;
break;
case "-";
$res = $num1 - $num2;
break;
case "*":
$res = $num1 * $num2;
break;
case "/":
$res = $num1 / $num2;
break;
default:
echo '運算符號錯誤!';
break;
}
return $res;
}
?>
URL:http://www.bianceng.cn/webkf/PHP/201609/50428.htm