_php tutorial on value passing and reference passing in PHP

Source: Internet
Author: User
Value passing and reference passing are present in PHP. The latter needs to use the address character & to identify the variable. Examples of using assignment operations are as follows:

1. Value passing

1) Basic Data type:

01
02
$a = 1;
03
$b = $a;
04
$b + = 2;
05

06
echo "\ $a =". $a. "
";
07
echo "\ $b =". $b. "
";
08

09
/* Output:
10
$a = 1
11
$b = 3
12
*/
13
?>
Note: The $b = $a statement is used here to assign a value of $ A to $b. That is, the value assigned at this time is 1.

Execute statement $b + = 2:

So, $b becomes 3, but the value of $ A does not change.

2) Reference data type

The person class is as follows:

01
Class Person {
02
Private $_name;
03

04
Public Function SetName ($name) {
05
$this->_name = $name;
06
}
07

08
Public Function GetName () {
09
return $this->_name;
10
}
11

12
Public Function toString () {
13
Return "name is". $this->_name;
14
}
15
}
Initialize the two Person class object and set the properties:

1
$p 1 = new person ();
2
$p 1->setname ("Person1");
3
$p 2 = new Person ();
4
$p 2->setname ("Person2");
Perform the following actions:

1
$p 3 = $p 1;
Test:

1
echo "\ $p 1 ' s". $p 1->tostring (). ".
";
2
echo "\ $p 3 ' s". $p 3->tostring (). ".
";
3

4
/* Output:
5
$p 1 ' s name is Person1.
6
$p 3 ' s name is Person1.
7
*/
Note: for $p 3 = $p 1 statement, the value assigned at this time is 0x000a. That is, both $P1 and $P3 refer to the same object.

2. Reference delivery

1) Basic Data type

01
02
$a = 1;
03
$b = & $a;
04
$b + = 2;
05

06
echo "\ $a =". $a. "
";
07
echo "\ $b =". $b. "
";
08

09
/* Output:
10
$a = 3
11
$b = 3
12
*/
13
?>
Note: The $b = & $a statement is used here to pass the reference: that is, the value assigned at this time is 0x0001.


So on $b + = 2,

So the value of $ A and $b is 3.

2) Reference data type

If you are performing the following actions to replace the previous $P3 = $p 1

1
$p 3 = & $p 1;
Then do the following:

1
$p 3 = $p 2;
The test results are as follows:

1
echo "\ $p 1 ' s". $p 1->tostring (). ".
";
2
echo "\ $p 3 ' s". $p 3->tostring (). ".
";
3

4
/* Output:
5
$p 1 ' s name is Person2.
6
$p 3 ' s name is Person2.
7
*/
Note: It is clear that the result information that has been printed out is $P3 because the $P3 = $p 2 statement is executed, but why is the result information printed $P1 different than before?

is because $P3 = & $p 1 statement is executed, that is, the value assigned at this time is 0x0001.


Immediately after executing $P3 = $p 2 statement


That's why the result information is printed as above, and this is where the address character & is working.

The complete code is as follows:

1) Reference data type


php
!--?/-->02
class Person {
$_name
Private;



Public Function SetName ($name) {

$this->_name = $name;
"
}
"

"
Public Function GetName () {
Ten
return $this->_name;

}


All
Public Function toString () {
+
Return ' name is '. $this->_name;

}

}
1


$p = new Person ();

$p 1->setname ("Person1");

$p 2 = new Person ();

$p 2->setname ("Person2");


3
$p = $p 1;

//$p 3 = & $p 1;

//$p 3 = $p 2;

echo \ $p 1 ' s ". $p 1->tostring ().".
";

echo \ $p 3 ' s. $p 3->tostring (). ".
";

;
2) basic data type



1
2
$a = 1;
3
$b = $a;
4
$b = & $a;
5
$b + = 2;
6

7
echo "\ $a =". $a. "
";
8
echo "\ $b =". $b. "
";
9
?>
Concluding remarks: For value passing and reference passing on method/function arguments, the condition is similar to the assignment operation.


http://www.bkjia.com/PHPjc/478049.html www.bkjia.com true http://www.bkjia.com/PHPjc/478049.html techarticle value passing and reference passing are present in PHP. The latter needs to use the address character to identify the variable. An example of using an assignment operation is as follows: 1, Value pass 1) Basic data type: £ º PHP $a ...

  • 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.