Reputation: 4421
What does a &
in front of a variable name mean?
For example &$salary
vs. $salary
Upvotes: 62
Views: 33140
Reputation: 1262
In the world of PHP, references are like magical mirrors, reflecting changes across your code. Let's unravel their mysteries with some whimsical examples:
We use &$var
instead of $var
when we want to pass a reference to the variable rather than its value. This allows changes made to the referenced variable within a function to affect the original variable outside the function, saving memory and improving performance, especially with large data structures. It's particularly useful when we need to modify variables directly within functions or when we want multiple functions to operate on the same variable.
The caution is to ensure that the referenced variable remains in scope and doesn't get unset or reassigned while its reference is still being used, to avoid unexpected behavior or errors. So Understand it before use it.
$hero = 'Harry'; // Our brave hero's name
$darkLord = &$hero; // The villain takes on the hero's guise
$darkLord = "Behold, I am $darkLord, the Dark Lord"; // Embracing darkness
echo $darkLord; // "Behold, I am Harry, the Dark Lord"
echo $hero; // Also echoes: "Behold, I am Harry, the Dark Lord"
Here, $darkLord and $hero share the same identity, revealing the duality of our characters.
$age = 25;
$referenceToAge = &$age; // Perfectly acceptable
$referenceToCalculation = &(24 * 7); // Forbidden incantation; referencing an unnamed expression
function getAge() {
return 25;
}
$referenceToFunction = &getAge(); // Spells backfire; can't directly reference a function return
function changeName(&$person) {
$person = 'Voldemort';
}
$name = 'Harry';
changeName($name); // Alas! The hero falls to the dark side
echo $name; // "Voldemort" rises, echoing across the realm
But beware! Passing anything other than a variable reference unleashes chaos.
$powerLevel = 9000; // Our hero's power level
$referenceToPowerLevel = &$powerLevel; // Power harnessing at its finest
$referenceToExpression = &(10 * 5); // Quest halted; can't reference an unnamed expression
function getPowerLevel() {
return 9000;
}
$referenceToFunction = &getPowerLevel(); // Journey thwarted; can't directly reference a function return
$teamA = ['Gandalf', 'Frodo', 'Aragorn']; // Fellowship of the Ring
$teamB = &$teamA; // Allies bound by fate
array_push($teamB, 'Legolas'); // A new member joins the fellowship
print_r($teamA); // The roster echoes across Middle-earth
print_r($teamB); // Both teams share the same quest
In the realm of arrays, references forge unbreakable bonds, uniting allies in their quest.
function &createEternalFlame() {
static $flame; // Eternal flame, ever-burning
if (!$flame) {
$flame = 'Eternal Flame'; // Igniting the essence of eternity
}
return $flame; // A beacon of hope for all who seek it
}
$torch = &createEternalFlame(); // Harnessing the essence of eternity
echo $torch; // "Eternal Flame" illuminates the darkness
With static references, legends are born, immortalizing their essence throughout time.
------------ LEARN MORE ------------
Upvotes: 2
Reputation: 1719
It is used to pass a variable by reference.
Read more in the PHP documentation.
Upvotes: -2
Reputation: 26740
It passes a reference to the variable so when any variable assigned the reference is edited, the original variable is changed. They are really useful when making functions which update an existing variable. Instead of hard coding which variable is updated, you can simply pass a reference to the function instead.
Example
<?php
$number = 3;
$pointer = &$number; // Sets $pointer to a reference to $number
echo $number."<br/>"; // Outputs '3' and a line break
$pointer = 24; // Sets $number to 24
echo $number; // Outputs '24'
?>
Upvotes: 88
Reputation: 25724
It's a reference, much like in other languages such as C++. There's a section in the documentation about it.
Upvotes: 3