Parameter transfer between php functions (value transfer/reference transfer)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.