The following code seems to do the same thing with and without ampersand( & ):
<?php
class Cartera {
public $dinero = 93;
public function &muestraLaPasta() { // Si quito este ampersand
return $this->dinero;
}
}
$carteraDeMaria = new Cartera;
$miPastaGansa = &$carteraDeMaria->muestraLaPasta(); // y este
echo $miPastaGansa; // el resultado es el mismo
?>
Whether I leave them or remove them the result is the same, it prints 93. What are they for?
Thanks to Ivan Botero's answer, I know that this function returns a reference.
But it seems that it is not a reference capable of surviving on its own. If I assign it to a variable, that variable contains the value, not the reference, despite the fact that what I have assigned to it is the result of that function, which is supposed to be a function that returns a reference.
<?php
class Cartera {
public $dinero = 93;
public function &muestraLaPasta() { // Esta función devuelve una referencia
return $this->dinero;
}
}
$carteraDeMaria = new Cartera;
$miPastaGansa = $carteraDeMaria->muestraLaPasta(); // Parecería que almaceno esa
//referencia en una variable
echo $miPastaGansa . "\n"; // el resultado es el mismo
// Cuanto dinero tiene Maria
echo $carteraDeMaria->dinero . "\n";
// Cuanto dinero tiene Maria ahora
$miPastaGansa = 5000; // Pero las modificaciones a través de esa referencia
// almacenada en esta variable
echo $carteraDeMaria->dinero . "\n"; // No se propagan
I think there is still some fine detail of that reference returned by the function that is escaping me.
How come I have to use another & before assigning it? It would seem that then I have a reference to the reference to money. Instead of just a reference to money, which is what I want.
To get started.
We must be clear about what a reference is in PHP.
According to a small snippet in the official PHP documentation .
Let's look at this example:
The result we will get will be:
But now, let's add an ampersand (&) to our code and see what happens:
The result we will get will be:
What does this tell us?
We have used an operator to access the same value of another variable and use it, we have modified it, but modifying it modified the value of the original variable, in this case
$variableA
.This is known as Allocation by Reference .
In that order of ideas
We can see a simple example of a function using references :
The result of this program will be:
And we use the ampersand operator (&) again and we will see how the magic happens.
And the new result is:
Like the previous example, what we have done here is to pass a reference to an external variable to the function, and through that reference we have modified it , increasing its value in each call to the function
caminar()
.We know this as Pass by Reference .
Finally
This is known as return referrals.
What does this mean?
Let's look at this example:
We have initialized the value of
$variable
with the text "StackOverflow", but when we run the program, we get the following output:What has happened is that in the variable
$puntero
we have saved a reference to the memory location of the variable$variable
, which contains our original text. Obtaining the reference to said variable makes any change we make to it affect the original variable .I have made a modification to your example, answering the question why it prints the same thing :
We get as a result:
What happened here?
When printing the return value of a reference function
muestraLaPasta()
, the value will always be left intact. What changes is the origin of that value, in that case, I have added this couple of lines:Modifying their reference
$miPastaGansa
, which in this case is a "pointer" to the attribute$dinero
. Which will give us the value of 5000 later, when we get the value of the attribute$dinero
.Little can actually be added to the excellent answer given.
I want to provide a visual explanation of what happens with references.
As said, references in PHP allow you to use two variables to refer to the same content. For this, the ampersand sign (
&
) is used, when I studied typing they called it: "and" commercial .References in PHP are not pointers , as they are in other languages, but would be like symbol table aliases.
We begin...
Caso 1:
Código:
Resultado:
Caso 2:
Código:
Resultado:
Caso 3:
Código:
Resultado
:When the reference is deactivated, the link between the variable name and the variable content is simply broken. This does NOT mean that the content of the variable will be destroyed, therefore, it
$x
recovers its original value when the reference is broken.Caso 4:
If you want to break the reference, the correct way is to use
unset
Código:
Resultado:
Caso 5:
We will try to break the reference by assigning
null
to the variable$x
and then assigning it a new value. Let's see what happens:Código:
Resultado:
The complete demo of the different cases can be found here .