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.