Page 1 of 1
What does these symbols mean???
Posted: Fri Oct 10, 2008 1:04 am
by deeessay
Hi what does this mean:
$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?

Re: What does these symbols mean???
Posted: Fri Oct 10, 2008 10:24 am
by alex.barylski
PHP Manual:
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.
Re: What does these symbols mean???
Posted: Fri Oct 10, 2008 3:33 pm
by panic!
here's a way to demonstrate it
Code: Select all
function addone($number)
{
$number++;
}
$x=1;
addone($number);
print $x; // prints 1
$x=1;
addone(&$number);
print $x; prints 2
when you use the & sign you're sending the actual variable itself into the function. Whereas without the & sign you're sending what the variables value is.
Re: What does these symbols mean???
Posted: Sat Oct 11, 2008 1:34 pm
by Ambush Commander
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.
Re: What does these symbols mean???
Posted: Sun Oct 12, 2008 10:27 am
by alex.barylski
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.
Just to add to this...PHP 5 passes objects around by reference however variable assignment does not follow this rule of thumb.
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.
In PHP 5 you need to clone objects when assigning them if you wish to make a copy:
http://ca.php.net/manual/en/language.oop5.cloning.php
Personally I still think it's good practice in PHP to use & in function arguments to indicate outgoing parameters. While it's not needed by the engine it makes it clear to the client developer and implementation developer of the purpose of the parameter.
Cheers,
Alex