Php: parameter transfer between functions
1. Value Transfer
Copy codeThe Code is as follows:
<? Php
Function exam ($ var1 ){
$ Var1 ++;
Echo "In Exam:". $ var1. "<br/> ";
}
$ Var1 = 1;
Echo $ var1. "<br/> ";
Exam ($ var1 );
Echo $ var1. "<br/> ";
?>
-------------------------------------------------------------------------------
Output result:
1
In Exam: 2
1
-------------------------------------------------------------------------------
2. Transfer references
Copy codeThe Code is as follows:
<? Php
Function exam (& $ var1 ){
$ Var1 ++;
Echo "In Exam:". $ var1. "<br/> ";
}
$ Var1 = 1;
Echo $ var1. "<br/> ";
Exam ($ var1 );
Echo $ var1. "<br/> ";
?>
-------------------------------------------------------------------------------
Output result:
1
In Exam: 2
2
-------------------------------------------------------------------------------
3. Optional parameters
Copy codeThe Code is as follows:
Function values ($ price, $ tax = ""){
$ Price + = $ prive * $ tax;
Echo "Total Price:". $ price. "<br/> ";
}
Values (100, 0.25 );
Values (1, 100 );
Output result:
Total Price: 125
Total Price: 100
-------------------------------------------------------------------------------
4. if an object is input, you can change the value of this object.
(In fact, the variable $ obj records the handle of this object. You can use $ obj as a parameter to operate the original object .)
Copy codeThe Code is as follows:
<? Php
Class Obj {
Public $ name;
Public $ age;
Public $ gander;
Public function _ construct ($ name, $ age, $ gander ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> gander = $ gander;
}
Public function show_info (){
Echo $ this-> name. "". $ this-> age. "". $ this-> gander. "<br/> ";
}
}
Function grow ($ obj ){
$ Obj-> age ++;
}
Function test (){
$ Obj = new Obj ("Mr. zhan", "12", "male ");
$ Obj-> show_info ();
Grow ($ obj );
$ Obj-> show_info ();
Grow ($ obj );
$ Obj-> show_info ();
}
Test ();
?>
-------------------------------------------------------------------------------
Output result:
Mr. zhan 12 male
Mr. zhan 13 male
Mr. zhan 14 male