The passage by reference I believe to be one of the coolest things that has in the programming.
I believe that one of the questions to be asked when studying this is :
Memoria
Imagine that when you are programming and do a variable assignment : $var = 5;,
you are not simply assigning 5 to a variable $var, you are booking a site
in the memory of your computer that will have as alias the name $var and he local will possess
the value 5.
0050 | 0051 | 0052 | 0053 | 0054 | 0055 | 0056 <-- possição da memoria
| | $var | | | | <-- alias
| | 5 | | | | <-- valor/referencia
The alias is actually just an access to the contents of the possibility 0052 of memory.
When you generate a passage by reference you are saying that the content of that
memory possibility is not a value, but a reference to a location that has the value.
It’s a little complicated but it would be basically like this :
$b = &$var;
0050 | 0051 | 0052 | 0053 | 0054 | 0055 | 0056 <-- possição da memoria
| | $var | | $b | | <-- alias
| | 5 | | 0052 | | <-- valor/referencia
Now when you access $b and will not display 0052, because he’s a reference,
it goes up to the reference and get its value in the case 5.
Thus if the content of the possibility 0052 is changed so much $var as $b evening
changed. Remembering that you can change both by $var as per $b.
$var = 7;
0050 | 0051 | 0052 | 0053 | 0054 | 0055 | 0056 <-- possição da memoria
| | $var | | $b | | <-- alias
| | 7 | | 0052 | | <-- valor/referencia
$b = 10;
0050 | 0051 | 0052 | 0053 | 0054 | 0055 | 0056 <-- possição da memoria
| | $var | | $b | | <-- alias
| | 10 | | 0052 | | <-- valor/referencia
Function
When you generate a function, the variables you create as a parameter generate a possible
in memory, if they are not of the reference type, they wait to receive a value, if
are of the reference type take the reference of the variable passed.
function teste($a){
}
0080 | 0081 | 0082 | 0083 | 0084 | 0085 | 0086 <-- possição da memoria
| | $a | | | | <-- alias
| | | | | | <-- valor/referencia
teste($var);
0080 | 0081 | 0082 | 0083 | 0084 | 0085 | 0086 <-- possição da memoria
| | $a | | | | <-- alias
| | 5 | | | | <-- valor/referencia
function teste(&$a){
}
0080 | 0081 | 0082 | 0083 | 0084 | 0085 | 0086 <-- possição da memoria
| | $a | | | | <-- alias
| | | | | | <-- valor/referencia
teste($var);
0080 | 0081 | 0082 | 0083 | 0084 | 0085 | 0086 <-- possição da memoria
| | $a | | | | <-- alias
| | 0052 | | | | <-- valor/referencia
Thus $a within the function changes the memory location 0052, changing the content definitively.
http://php.net/manual/en/language.references.pass.php
– Diego Souza
@Zoom the first thing I did was look at the documentation and as I did not understand I decided to ask to try to find the explanations in other.
– RFL
Got it, Bro. All right... ! There’ll be a good answer soon.
– Diego Souza
From what you said, you didn’t quite understand the workings of
return, not the references. Thereturnis more important and useful to understand first. No use returning "to anyone".– bfavaretto
Related http://answall.com/questions/48777/howfunction-a-refer%C3%Aancia-de-vari%C3%A1veis-n%C3%A3o-declares-no-php
– Wallace Maxters
This tag also has something to do with pass-by-reference
– Wallace Maxters