Quick Question: What does "=&" do?

Simply question but I can’t google it, google just ignores the entry and thinks I’m asking about ampersands.

I could be wrong but I dont think it does anything in PHP

they arent listed with php operators either

are you sure your not getting those symbols from the url?

See Assignment by Reference, PHP: Assignment Operators - Manual
* When dealing with PHP use the official manual, not one from w3schools.

Well I’m just looking at the Wordpress code to try and get an understanding of how CMS work and the trail I’ve followed led me to a function that contains this.

	if (is_object( $args))							//Check if $args is an object
		$object = get_object_vars($args);			//Assign the accessible objects of $args to $object.  Objects with no value return NULL
	elseif (is_array($args))						//Check if $args is an array
		$object =& $args;	

The bottom line containing the offending item.

I also looked on assignment operators but couldn’t find this operator.

It is Assignment by Reference. It can be written as “$var =& val;” or “$var = &val;

Ah ok, I think I understand. So it is performing an alteration if it is an object, otherwise it just assigns $object to mean the exact same thing so it can use $object later?

Might be a stupid question but why not just have:

$object = $args (without the &)?

Logic earth is correct, and that’s some pretty smelly code because the $object var gets set as an array either way. I don’t have a clue why you would ever name an array “object”

It is Wordpress, it is full of crap code.


Ok well thank you for the input, I’ll change this title to solved.

As an aside, is there a better system to observe then for developing a CMS?

EDIT: Nevermind I can’t change it to Solved.

$obj1 = new MyObject(); // Allocates memory for a new object
$obj1->a = "foo";
print($obj1->a); // prints "foo"

$obj2 = $obj1; // Allocates more memory to create obj2, which is a clone of obj1
$obj2->a = "bar";
print($obj1->a); // prints "foo"
print($obj2->a); // prints "bar"

$obj3 =& $obj1; // Doesn't allocate memory for another object, instead obj3 now is a pointer to obj1
$obj3->a = "foobar";
print($obj1->a); // prints "foobar"
print($obj2->a); // prints "bar"
print($obj3->a); // prints "foobar"

Ah OK, memory that makes sense. And $obj3 alters the contents of $obj1. Thats a lot clearer than the PHP.net example, thank you.

Think of variables as postal addresses.

An address references a real world location. A variable references a space in your computer’s memory (also called a memory “address”).

A reference is like a forwarding address - it’s not a real location in memory, but just redirects to another address.

Any use of the reference address is actually forwarded to the address it’s referencing.

Thanks, the examples you guys have provided has solved in seconds what the “professional” examples were useless for.

Some things about references.

First is the related (and far more useful) ability to iterate by reference.

foreach ($array as $key => &$value) {
  $value['newProperty'] = 'something';
  $value['changed'] = true;

It allows you to traverse an array or object and modify the members, then have those changes retained after the foreach loop is concluded.

You can also receive arguments by reference

function inc(&$myArgument) {

$a = 1;
echo $a; // 2

Be careful with this. It can lead to nighmarishly difficult to maintain code and is rarely (if ever) worth doing.

Finally, Keep in mind that all variables to objects are references.

$a = new myClass();

$b = $a;
// Is functionally identical in all ways to...
$b = &$a;
// or
$b =& $a;

This can (and has) thrown off many an intermediate programmer and caused hours of headaches. In Javascript every variable is a reference.

Finally, if you are coming to PHP from C++ then don’t confuse references for pointers. They are similar and used in many of the same situations, but there are fundemental differences that can cause trouble.

Also keep in mind that PHP’s garbage collection will not delete a value or object until all reference to it are deleted. Recursive references between objects are notoriously hard for garbage collectors in all languages to deal with and can cause memory leaks, so keep that in mind as well.