|
<?php
/*
* 无返回值函数,完成特定功能
*/
function printDate() {
//echo date("Y-m-d H:i:s");
echo date('Y-m-j');
}
echo '现在时间为:';
printDate();
/*
* 有返回值函数,php不支持函数重载
*/
function Sum($a,$b){
return $a+$b;
}
echo '<br/>5与2两个数的和为:'.Sum(5, 2);
/*
*值传递
*/
$string='hello';
function say($str){
$str.=' all';
echo $str;
}
//function say(){
//global $string;
//$string.=' all';
//echo $string;
//}
echo '<br/>';
say($string);
//say();
echo '<br/>'.$string;
/*
* 引用传递
*/
$a='a';
function change(&$str){
$str.='修改实参';
}
change($a);
echo '<br/>'.$a;
/*
* 默认参数
*/
function cook($str='早上好'){
return '大家'.$str;
}
echo '<br/>'.cook();
echo '<br/>'.cook('晚上好');
/*
* 可选参数
*/
function args(){
echo '<br/>参数的个数为:'.func_num_args();
if(func_num_args()>=3){
echo '<br/>第三个参数是:'.func_get_arg(2);
}
$temp=func_get_args();
for ($i=0;$i<func_num_args();$i++){
echo '<br/>第'.$i.'个参数是:'.$temp[$i];
}
}
args(1,2,3,4);
/*
* 返回数组
*/
function arrays(){
$user[]='西门吹雪';
$user[]='男';
return $user;
}
list($name,$sex)=arrays();
echo '<br/>姓名:'.$name.' 性别:'.$sex;
?>
<?php
function say($mday){
if ($mday>15){
echo '下半月';
}else{
echo '上半月';
}
}
$date=getdate();
echo checkdate($date['mon'], $date['mday'], $date['year'])?say($date['mday']):'no';
?> |
|
|