COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
$result=$number1-$number2;
break;
case
"*":
$result=$number1*$number2;
break;
case
"/":
if($number2!=0)
$result=$number1/$number2;
else
$result=0;
break;
case
"%":
if($number2!=0)
$result=$number1%$number2;
else
$result=0;
break;
}
return
$result;
}
echo "result of default operator: ".getResult(10,20);
echo
"<br>";
echo "result of * operator: ".getResult(10,20,"*");
?>
</BODY>
</HTML>
Neáu muoán ñònh nghóa function khoâng coù giaù trò traû veà, baïn coù theå khai baùo trong trang
void.php nhö ví duï sau:
...
function
calloperator()
{
echo "result of default operator: ".getResult(10,20);
echo
"<br>";
echo "result of * operator: ".getResult(10,20,"*");
}
calloperator();
?>
</BODY>
</HTML>
Trong tröôøng hôïp truyeàn tham soá nhö tham bieán, baïn söû duïng kyù hieäu & tröôùc tham soá,
chaúng haïn chuùng ta khai baùo haøm coù tham bieán coù teân average nhö trong trang
reference.php nhö sau:
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Function</h4>
<?php
function
getAmount($quantity,
$price,&$average)
{
$result=0;
$result=$quantity*$price;
$average=$result*6/12;
return
$result;
}
$bq=0;
echo "result is : ".getAmount(10,20,$bq);
echo
"<br>";
Giaùo vieân: Phaïm Höõu Khang