$var =& new Swift_Message("blah blah blah");
okay the example used Swift Mailer but what I don't understand is the "=&" part
what does it mean anyway?
Moderator: General Moderators
By default, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see the chapter on Expressions.
PHP also offers another way to assign values to variables: assign by reference. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa.
Code: Select all
function addone($number)
{
$number++;
}
$x=1;
addone($number);
print $x; // prints 1
$x=1;
addone(&$number);
print $x; prints 2
Just to add to this...PHP 5 passes objects around by reference however variable assignment does not follow this rule of thumb.Ambush Commander wrote:Please note that doing =& new only makes sense in PHP4; in PHP5 objects are passed "by reference" automatically (it's a little more complicated than that, but that term will suffice for now) and so the ampersand is unnecessary and will result in warnings.
In PHP 5 you need to clone objects when assigning them if you wish to make a copy:http://ca.php.net/language.variables wrote:By default, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see the chapter on Expressions.
PHP also offers another way to assign values to variables: assign by reference. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa.