PHP variable reference, function reference, object reference Three reference instance code detailed

Source: Internet
Author: User
What is a reference?

Referencing in PHP means accessing the same variable content with a different name. This is not like the C pointer, instead, the reference is the symbol table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix filename and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be seen as hardlink in Unix file systems.
A: A reference to a variable

<?php$a =100; $b = & $a; echo $b;    Output 100echo $a here;    Output here  , stating $ A, and the value of $b are 100. $b = 200;echo $a;    Output 200echo $b here;    Output 200 here, it can be seen that they are using the same address. Change one, and the other will change.?>

Two: reference value in function

<?phpfunction main ($a, $b) {    $b = $a +100;    return $b;} Main (55,& $b);      The $b here is to pass its memory address to the $b parameter in the function main, changing the value of the outside $b by changing the parameter $b. echo $b;  This will output 155,?>


Three: The reference value of the object
References to Objects

<?phpclass Club{var $name = "Real Madrid";} $b =new Club; $c = $b; echo $b->name;//here output real Madridecho $c->name;//here output real madrid$b->name= "Ronaldo"; Echo $c- >name;//here output ronaldo?>

Dereference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

<?php$a = ' Ronaldo ' $b =& $a; unset ($a);? >

Not unset $b, just $a.

Related Article

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.