Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "&" sign mean in PHP? [duplicate]

Tags:

php

I was trying to find this answer on Google, but I guess the symbol & works as some operator, or is just not generally a searchable term for any reason.. anyhow. I saw this code snippet while learning how to create WordPress plugins, so I just need to know what the & means when it precedes a variable that holds a class object.

//Actions and Filters
if (isset($dl_pluginSeries)) {

    //Actions
    add_action('wp_head', array(&$dl_pluginSeries, 'addHeaderCode'), 1);
    //Filters
    add_filter('the_content', array(&$dl_pluginSeries, 'addContent'));
}
like image 361
jeffkee Avatar asked Mar 11 '10 06:03

jeffkee


People also ask

What the Fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What is this song Google?

Ask Google Assistant to name a song On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.

What does the fox say for real?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.


2 Answers

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

$a = 1;

function inc(&$input)
{
   $input++;
}

inc($a);

echo $a; // 2

Objects will be passed by reference automatically.

If you like to handle a copy over to a function, use

clone $object;

Then, the original object is not altered, eg:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1
like image 95
Phil Rykoff Avatar answered Sep 22 '22 20:09

Phil Rykoff


The ampersand preceding a variable represents a reference to the original, instead of a copy or just the value.

See here: http://www.phpreferencebook.com/samples/php-pass-by-reference/

like image 28
Mike Cialowicz Avatar answered Sep 26 '22 20:09

Mike Cialowicz