What does these symbols mean???

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
deeessay
Forum Commoner
Posts: 55
Joined: Sat May 24, 2008 1:02 am

What does these symbols mean???

Post 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?

:?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: What does these symbols mean???

Post 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.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What does these symbols mean???

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: What does these symbols mean???

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: What does these symbols mean???

Post 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
Post Reply