- 论坛徽章:
- 0
|
以下代码来自于《php与MySQL5程序设计》一书,P67~P74,学习调试后没地方放,就放上面吧!
?php
//函数 @date 2007.11.5
//调用函数
$value = pow(5,3); //计算5的3次方
echo $value;
//创建函数
function generate_footer() {
echo "Copyright © 2008";
}
generate_footer();
//按值传递参数
function salestax($price,$tax) {
$total = $price + ($price * $tax);
return $total;
}
$total = salestax(100,.20);
echo $total;
//按引用传递参数
$cost = 20.00;
$tax = 0.05;
function calculate_cost (&$cost,$tax) {
$cost = $cost + ($cost * $tax);
$tax += 4;
}
calculate_cost($cost,$tax);
echo "Tax is :" . $tax * 100 ."\n"; // Tax is :5
echo "Cost is :" . $cost . "\n"; // Cost is :21
// 如果将&$cost前的&去掉,则输出 Cost is :20
//默认参数值
function salestax($price,$tax=0.02) {
$total = $price + ($price * $tax);
echo "$total";
}
salestax(100); //102
salestax(100,0.01); //101
//可选参数
function salestax($price,$tax="") {
$total = $price + ($price * $tax);
echo $total;
}
//salestax(100); //100
salestax(100,0.02); //102
//从函数返回多个值
function retrieve_user_profile() {
$user[] = "Mr.Bool";
$user[] = "Mr.Bool@yahoo.cn";
$user[] = "English";
return $user;
}
list($name,$email,$language) = retrieve_user_profile();
echo "Name:$name \n";
echo "Email:$email \n";
echo "Language:$language \n";
?>
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/51625/showart_415158.html |
|