现在的位置: 首页 > 综合 > 正文

php学习4 函数定义

2012年09月17日 ⁄ 综合 ⁄ 共 2761字 ⁄ 字号 评论关闭
1 <?php
2 //1,函数返回值
3 function validatelogin($username,$password){
4 $actualuser="xxx";
5 $actualpass="xxx";
6 if (strcmp($username,$actualuser)==0&&strcmp($password,$actualpass)==0){
7 returntrue;
8 }else{
9 returnfalse;
10 }
11 }
12
13 //strcmp(str1,str2) 二进制安全字符串比较
14 //如果 str1 小于 str2,返回负数;如果 str1 大于 str2,返回正数;二者相等则返回 0。
15
16 //2,函数参数设置默认值
17 //如果参数没有设置,不会报错,会使用默认值完成调用。
18
19 function addvalues($value1=0,$value2=0,$value3=0){
20 $total=$value1+$value2+$value3;
21 return$tatal;
22 }
23
24 //3 按引用传递和按值传递
25 function attachtext(&$newtext=""){
26 $newtext=$newtext."world";
27 }
28
29 function attachtext2($newtext=""){
30 $newtext=$newtext."world";
31 }
32 $mystring="hello";
33 $mystring2="hello";
34 attachtext($mystring);
35 attachtext2($mystring2);
36 echo$mystring."<br>";// helloword
37 echo$mystring2."<br>";//hello
38
39 //可变参数
40 function addanything(){
41 $total=0;
42 $args=func_get_args();
43 for($i=0;$i<count($args);$i++){
44 if(is_int($args[$i])){
45 $total+=$args[$i];
46 }
47 }
48 return$total;
49 }
50 echo addanything(1,2,6)."<br>";//9
51 echo addanything(1,1);//2
52 //func_get_args(),func_num_args(),func_get_arg()
53 //注意,这三个函数只能用在函数内部;否则会报错;
54
55 //$number = func_num_args(); 返回INT
56 //返回函数调用时,给出参数的数量;
57
58 //$para = func_get_arg(n);
59 //可以返回第N-1位参数的值,因为引索的起始是0
60
61 //$arr = func_get_args();
62 //这个是用来返回参数的数组
63
64 //返回多个值
65 function addandsub($f1,$f2){
66 $rf1=($f1+$f2);
67 $rf2=($f1-$f2);
68
69 $myarray=array();
70 $myarray[0]=$rf1;
71 $myarray[1]=$rf2;
72 return$myarray;
73 }
74
75 $my=array();
76 $my=addandsub(10,3);
77 echo$my[0]."<br>";
78 echo$my[1];
79
80
81
82 class myclass{
83 private$thevalue;
84 private$theword;
85
86 publicfunction _construct(){
87 $num_args=func_num_args();
88 if ($num_args>0){
89 $args=func_num_args();
90 $this->theword=$args[0];
91 }
92 }
93 publicfunction setvalue($newvalue){
94 $this->thevalue=$newvalue;
95 }
96 publicfunction getvalue(){
97 return$this->thevalue;
98 }
99 publicfunction getword(){
100 return$this->theword;
101 }
102 }
103 $myclass1=new myclass("Abra");
104 $myclass1->setvalue(1);
105
106 $myclass2=new myclass("Kadabra");
107 $myclass2->setvalue(2);
108
109 $myclass3=new myclass("Hocus");
110 $myclass3->setvalue(3);
111
112 $myclass4=new myclass("Pocus");
113 $myclass4->setvalue(4);
114
115 $classarr=array($myclass1,$myclass2,$myclass3,$myclass4);
116
117 function&findclass($whichclass,$classarr){
118 for ($i=0;$i<count($classarr);$i++){
119 if ($classarr[$i]->getvalue()==$whichclass){
120 return$classarr[$i];
121 }
122 }
123 }
124 $myobject=new myclass("");
125 $myobject=&findclass(2,$classarr);
126 echo$myobject->getword();
127
128
129 //调用变量函数
130 function addvalue($f1=0,$f2=0){
131 return$f1+$f2;
132 }
133
134 function subvalue($f1=0,$f2=0){
135 return$f1-$f2;
136 }
137 function multivalue($f1=0,$f2=0){
138 return$f1*$f2;
139 }
140
141 $f1=10;
142 $f2=3;
143
144 $whatdo="addvalue";
145 echo$whatdo($f1,$f2)."<br>";
146
147 $whatdo="subvalue";
148 echo$whatdo($f1,$f2)."<br>";
149
150
151
152 $whatdo="multivalue";
153 echo$whatdo($f1,$f2)."<br>";
154
155 ?>

抱歉!评论已关闭.