8

I'm 'dissecting' PunBB, and one of its functions checks the structure of BBCode tags and fix simple mistakes where possible:

function preparse_tags($text, &$errors, $is_signature = false)

What does the & in front of the $error variable mean?

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
john mossel
  • 2,158
  • 5
  • 24
  • 39
  • possible duplicate of [PHP: What does a & in front of a variable name mean?](http://stackoverflow.com/questions/3774130/php-what-does-a-in-front-of-a-variable-name-mean) – nbro Jul 24 '15 at 11:06

4 Answers4

27

It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.

function passByReference(&$test) {
    $test = "Changed!";
}

function passByValue($test) {
    $test = "a change here will not affect the original variable";
}

$test = 'Unchanged';
echo $test . PHP_EOL;

passByValue($test);
echo $test . PHP_EOL;

passByReference($test);
echo $test . PHP_EOL;

Output:

Unchanged

Unchanged

Changed!

Community
  • 1
  • 1
Andy
  • 17,423
  • 9
  • 52
  • 69
2

It does pass by reference rather than pass by value.

This allows for the function to change variables outside of its own scope, in the scope of the calling function.

For instance:

function addOne( &$val ) {
    $val++;
}
$a = 1;
addOne($a);
echo $a; // Will echo '2'.

In the case of the preparse_tags function, it allows the function to return the parsed tags, but allow the calling parent to get any errors without having to check the format/type of the returned value.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
1

It accepts a reference to a variable as the parameter.

This means that any changes that the function makes to the parameter (eg, $errors = "Error!") will affect the variable passed by the calling function.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

It means that the variable passed in the errors position will be modified by the called function. See this for a detailed look.

JadziaMD
  • 2,690
  • 5
  • 31
  • 42