Five PHP instances, detailing the differences between passing values and transferring references

Source: Internet
Author: User

 

[Transferred from the sea-bottom cangying Tank Http://blog.51yip.com/php/878.html ]

 

Just learnedPHPAt that time, I had done a lot of projects and made a lot of things, so I thought I was myselfPHPI have mastered almost the same thing. Over time, the deeper I learned, the more I felt that I knew little about it. Haha, it will only be used in the preliminary stage. We need to understand the principle so that we can use it better.

Pass value: Assign the value of the real parameter to the row parameter. The modification of the row parameter does not affect the value of the real parameter.

Upload reference : After passing parameters in the address mode, the row parameters and real parameters are the same object, but their names are different. Modifications to the row parameters will affect the values of the real parameters.

Note:

Pass value: RootCopyIs the same. For example, if I have a house, I will give you building materials, and you have built a house with the same root of my house, what you do in your house will not affect me, what I do in my house will not affect you and be independent of each other.

Reference: reminds me of studying in college.CThe pointer to the language is almost the same. For example, if I have a house, and I give you a key, both of us can enter the house. What you do in the house will affect me.

I,PHPInstance

1, Pass value 

<? PHP
$ Param1 = 1; // Define variable 1
$ Param2 = 2; // Define variable 2
$ Param2 = $ Param1 ; // Variable 1 is assigned to variable 2.
Echo $ Param2 ; // Displayed as 1

?>

2, Upload reference

<? PHP
$ Param2 = 1; // Define variable 2
$ Param1 = & $ Param2 ; // Pass the reference of variable 2 to variable 1
Echo $ Param2 ; // Displayed as 1
$ Param1 = 2; // Assign 2 to variable 1
Echo $ Param2 ; // Displayed as 2

?>  

 

3, Function Pass Value

<? PHP

//Pass Value

$ Param1= 1;//Define variable 1

FunctionAdd ($ Param2)//PASS Parameters{

$ Param2 = 3; // Assign 3 to variable 2
}
$ Param3 = Add ( $ Param1 ); // Call the add method and pass variable 1 to variable 2.
Echo '<Br> $ param1 = '. $ Param1 . '<Br> '; // Displayed as $ param1 = 1
Echo '<Br> $ param2 = '. $ Param2 . '<Br> '; // Shown as $ param2 = because $ param2 is a local variable, it cannot affect the global
Echo '<Br> $ param3 = '. $ Param3 . '<Br> '; // Displayed as $ param3 = because the add method does not return a value, $ param3 is empty
?>

4, Function transfer reference

  <? PHP

//Pass Value

$ Param1= 1;//Define variable 1

Function Add (& $ Param2 ) // PASS Parameters {
$ Param2 = 3; // Assign 3 to variable 2
Return $ param2; // return variable 2
}
Echo '<Br> $ param1 = '. $ Param1 . '<Br> '; // No operation is performed on variable 1 as shown in $ param1 = 1
$ Param3 = Add ( $ Param1 ); // Call the add method and pass the reference of variable 1 to variable 2.
Echo '<Br> $ param1 = '. $ Param1 . '<Br> '; // When $ param1 = 3 is called, $ param2 changes affect variable 1, although no return
Echo '<Br> $ param2 = '.$ Param2 . '<Br> '; // Shown as $ param2 = because $ param2 is a local variable, global variables cannot be affected.
Echo '<Br> $ param3 = '. $ Param3 . '<Br> '; // Displayed as $ param3 = $ param3 = 3 if the return comment in the method is removed
?>

5, Function transfer reference2

<? PHP

// Upload reference $ Param1 = 1;
Function & Add (& $ Param2 )
{
$ Param2 = 2;
Return $ Param2 ;
}
$ Param3 = & Add ( $ Param1 );
$ Param4 = Add ( $ Param1 );
Echo '<Br> $ param3 = '. $ Param3 . '<Br> '; // Displayed as $ param3 = 2
Echo '<Br> $ param4 = '. $ Param4 . '<Br> '; // Displayed as $ param4 = 2
Echo '<Br> $ param1 = '. $ Param1 . '<Br> '; // It is displayed as $ param1 = 2. Changes in $ param2 affect variable 1

$ Param3 ++;

/* $ param1 = 3 is shown below, because $ param2 and $ param1 reference to the same place,
* the return value is preceded by an address symbol or a reference $ param3 = & add ($ param1);
* in this way, $ param3, $ param2 and $ param1 are referenced in the same place. When $ param3 ++,
* $ param1 is changed */
echo '
$ param1 = '. $ param1 . '
';

$ Param4++;
/*The following figure shows $ param1 = 3. Why is it 3 instead of 4? This is because there is no
* The address symbol, which is not a reference. Therefore, $ param1 is not affected when $ param4 is changed.*/
Echo'<Br> $ param1 = '.$ Param1. '<Br> ';
?>

 2. Faster value transfer and reference Transfer

I did a test. The test file is as follows:

1, Test FileTest1 --Upload reference

<? PHP

$ Param=Array(, 28 );
Foreach($ Param As $ K=> &$ V){
 $ V++;
}
?>

 2. Test File Test2 -- pass value

<? PHP
$ Param = Array (, 28 );
Foreach ( $ Param As $ K => $ V ){
$ V ++;
$ Param [ $ K ] = $ V ;
}
?>

 

 

 

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.