45

I've been googling but I can't find anything.

$x->func(&$string, $str1=false, $str2=false);

what does that & before $string &$string do?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Mickey
  • 2,285
  • 6
  • 26
  • 37
  • 7
    Whoever marked this as a duplicate: this is currently INCORRECT ... this question is NOT a duplicate, as the combination of symbols "&$" is NOT show in the so-called referenced question. Please show more care and consideration in marking questions as duplicates. – mike rodent Mar 28 '15 at 10:52
  • Mike is right. It has not been answered, at least not in the indicated page. This kind of replies are irresponsible. They should be banned and the persons making them should be downgraded. – Apostolos Apr 09 '17 at 07:26

5 Answers5

59

You are assigning that array value by reference.

passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this copy, so if you change it in the function it won't be changed outside of it

Ref: http://www.php.net/manual/en/language.references.pass.php

Krish R
  • 22,583
  • 7
  • 50
  • 59
15

The & states that a reference to the variable should be passed into the function rather than a clone of it.

In this situation, if the function changes the value of the parameter, then the value of the variable passed in will also change.

However, you should bear in mind the following for PHP 5:

  • Call time reference (as you have shown in your example) is deprecated as of 5.3
  • Passing by reference when specified on the function signature is not deprecated, but is no longer necessary for objects as all objects are now passed by reference.

You can find more information here: http://www.php.net/manual/en/language.references.pass.php

And there's a lot of information here: Reference — What does this symbol mean in PHP?

An example of the behaviours of strings:

function changeString( &$sTest1, $sTest2, $sTest3 ) {
    $sTest1 = 'changed';
    $sTest2 = 'changed';
    $sTest3 = 'changed';
}

$sOuterTest1 = 'original';
$sOuterTest2 = 'original';
$sOuterTest3 = 'original';

changeString( $sOuterTest1, $sOuterTest2, &$sOuterTest3 );

echo( "sOuterTest1 is $sOuterTest1\r\n" );
echo( "sOuterTest2 is $sOuterTest2\r\n" );
echo( "sOuterTest3 is $sOuterTest3\r\n" );

Outputs:

C:\test>php test.php

sOuterTest1 is changed
sOuterTest2 is original
sOuterTest3 is changed
Kingsley
  • 977
  • 2
  • 11
  • 27
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
5

& means passing by reference:

References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand (&) is placed before the variable to be referenced.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
LightningBoltϟ
  • 1,383
  • 2
  • 10
  • 28
3

It means you pass a reference to the string into the method. All changes done to the string within the method will be reflected also outside that method in your code.

See also: PHP's =& operator

Example:

$string = "test";
$x->func(&$string); // inside: $string = "test2";
echo $string; // test2

without the & operator, you would still see "test" in your variable.

Community
  • 1
  • 1
eX0du5
  • 896
  • 7
  • 16
  • funny you mentioned that "=&", I just saw and learned what that was tonight, i've been coding for years and have never used it before. – Mickey Dec 06 '13 at 08:56
  • I have also coded php for years but only rarely used it. Most of the times you are passing objects and do not need this. – eX0du5 Dec 06 '13 at 08:58
3

& – Pass by Reference. It is passed by reference instead of string value.

user3069029
  • 211
  • 2
  • 10