Asim Zaidi
Asim Zaidi

Reputation: 28344

what does & do in php

I have this code

$myNewClass->cars =& Orders_Car::GetRecords($myNewClass->searchString);
                   ^

what is & doing there. thanks

Upvotes: 3

Views: 636

Answers (4)

Bill Karwin
Bill Karwin

Reputation: 562911

Read Do not use PHP references by PHP expert Johannes Schlüter

PHP references are a holdover from PHP 4, where objects were passed by value instead of by reference unless you deliberately made them by reference. This isn't necessary when using PHP 5, because all objects are passed by reference in PHP 5 all the time.

Scalars and arrays are still passed by value by default in PHP 5. If you need to pass them by reference, you should just use an object.

Upvotes: 4

JAL
JAL

Reputation: 21563

It changes the variable to be 'passed by reference'. This is as opposed to passing a copy of the variable in.

See the manual for details.

One common use is in foreach.

Typically, foreach operates on a copy of the iterable array or object.

That means

$salad=array(1,2,3);
foreach ($salad as $leaf){
  $leaf+=1;
  }
echo $salad[0];// output: 1   

foreach ($salad as &$leaf){
  $leaf+=1;
  }
echo $salad[0];// output: 2 because & made the assignment affect the original array

Upvotes: 6

greg0ire
greg0ire

Reputation: 23265

It creates a reference. If you don't know what a reference is, read this : http://www.php.net/manual/en/language.references.php

Upvotes: 6

Related Questions