Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meaning of &$variable and &function? [duplicate]

Tags:

php

Possible Duplicate:
Reference - What does this symbol mean in PHP?

what is the meaning of &$variable
and meaning of functions like

function &SelectLimit( $sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0 )
{
    $rs =& $this->do_query( $sql, $offset, $nrows, $inputarr);
    return $rs;
} 
like image 781
Mostafa Darwish Avatar asked Oct 21 '12 21:10

Mostafa Darwish


People also ask

What symbol means?

1. something used for or regarded as representing something else; a material object representing something, often something immaterial; emblem, token, or sign. 2. a letter, figure, or other character or mark or a combination of letters or the like used to designate something.

What meaning means?

1a : the thing one intends to convey especially by language : purport Do not mistake my meaning. b : the thing that is conveyed especially by language : import Many words have more than one meaning. 2 : something meant or intended : aim a mischievous meaning was apparent.

When meaning in meaning?

\ ˈ(h)wen , (h)wən \ Definition of when (Entry 2 of 4) 1a : at or during the time that : while went fishing when he was a boy.

What is a opposite of?

An antonym is a word that is the opposite of another word. An opposite can be the other side of, reverse of, or something contrary to anything, not just words.


2 Answers

Passing an argument like so: myFunc(&$var); means that the variable is passed by reference (and not by value). So any modifications made to the variable in the function modify the variable where the call is made.

Putting & before the function name means "return by reference". This is a bit very counter-intuitive. I would avoid using it if possible. What does it mean to start a PHP function with an ampersand?

Be careful not to confuse it with the &= or & operator, which is completely different.

Quick test for passing by reference:

<?php
class myClass {
    public $var;
}

function incrementVar($a) {
    $a++;
}
function incrementVarRef(&$a) { // not deprecated
    $a++;
}
function incrementObj($obj) {
    $obj->var++;
}

$c = new myClass();
$c->var = 1;

$a = 1; incrementVar($a);    echo "test1 $a\n";
$a = 1; incrementVar(&$a);   echo "test2 $a\n"; // deprecated
$a = 1; incrementVarRef($a); echo "test3 $a\n";
        incrementObj($c);    echo "test4 $c->var\n";// notice that objects are
                                                    // always passed by reference

Output:

Deprecated: Call-time pass-by-reference has been deprecated; If you would like
to pass it by reference, modify the declaration of incrementVar(). [...]
test1 1
test2 2
test3 2
test4 2
like image 131
Benjamin Crouzier Avatar answered Sep 19 '22 05:09

Benjamin Crouzier


The ampersand - "&" - is used to designate the address of a variable, instead of it's value. We call this "pass by reference".

So, "&$variable" is the reference to the variable, not it's value. And "function &func(..." tells the function to return the reference of the return variable, instead of a copy of the variable.

See also:

  • difference between function and &function
  • http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference
  • http://www.php.net/manual/en/language.references.pass.php
  • http://www.adp-gmbh.ch/php/pass_by_reference.html
like image 45
cegfault Avatar answered Sep 21 '22 05:09

cegfault