PHP: Only variable references should be returned by reference

There is a mobile optimized version of this page, view AMP Version.

Pulling your hair out over this Notice message? Well here is an explanation for why you are getting this and how you can avoid it.

Take a look at the following code:

<?php
class myClass {
    public $var = 1;
 
    public function &getVar() {
        return($this->var);
        }
}
 
$obj = new myClass;
$value = &$obj->getVar();
?>

Now this code works completely fine, apart from every time it hits the “return” statement it chucks out a weird Notice error: Only variable references should be returned by reference.

But what does that actually mean? … Basically the return statement is returning the result of the variable and not the reference because of the () brackets around the variable.

so to return correctly by reference you would do this instead:

<?php
class myClass {
    public $var = 1;
 
    public function &getVar() {
        return $this->var;
        }
}
 
$obj = new myClass;
$value = &$obj->getVar();
?>

Kicking yourself now? Yes it is that easy…

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

  • Rolando

    Greetings, I ran into the same problem, but was using the set, it had been running fine until the Notice were activated.

    This was
    "public function &setRutaUpdate($ RutaUpdate){
    return $this->RutaUpdate = $RutaUpdate;
    }"

    I do not understand why the message, but I corrected basing your code and adding

    "public function &setRutaUpdate($ RutaUpdate){
    $this->RutaUpdate = $ RutaUpdate;
    return $this->RutaUpdate;
    }"

    Well, I hope someone seeking this solution can help.

  • AndreaB.

    hi!
    my error is:
    Notice: Only variable references should be returned by reference in /home/vn000150/public_html/admin/mailings/func/chron/htmlMimeMail.php on line 440

    the code is:

    function &_addAlternativePart(&$obj)
    {
    $params['content_type'] = 'multipart/alternative';
    if (is_object($obj)) {
    return $obj->addSubpart('', $params);
    } else {
    return new Mail_mimePart('', $params);
    }
    }

    error line: return new Mail_mimePart('', $params);

    do you know how fixed?

    I have the same error other 2 times:
    1.
    the error is:
    Notice: Only variable references should be returned by reference in /home/vn000150/public_html/admin/mailings/func/chron/smtp.php on line 94
    function &connect($params = array()){
    if(!isset($this->status)){
    $obj = new smtp($params);
    if($obj->connect()){
    $obj->status = SMTP_STATUS_CONNECTED;
    }
    return $obj;
    }else{
    $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
    if(function_exists('socket_set_timeout')){
    @socket_set_timeout($this->connection, 5, 0);
    }
    $greeting = $this->get_data();
    if(is_resource($this->connection)){
    return $this->auth ? $this->ehlo() : $this->helo();
    }else{
    $this->errors[] = 'Failed to connect to server: '.$errstr;
    return FALSE;
    }
    }
    }
    The error is on the last if:
    return $this->auth ? $this->ehlo() : $this->helo();

    2.
    function &_addHtmlImagePart(&$obj, $value)
    {
    $params['content_type'] = $value['c_type'];
    $params['encoding'] = 'base64';
    $params['disposition'] = 'inline';
    $params['dfilename'] = $value['name'];
    $params['cid'] = $value['cid'];
    $obj->addSubpart($value['body'], $params);
    }
    the error is en the last }
    and appear two times together
    Notice: Only variable references should be returned by reference in /home/vn000150/public_html/admin/mailings/func/chron/htmlMimeMail.php on line 468
    Notice: Only variable references should be returned by reference in /home/vn000150/public_html/admin/mailings/func/chron/htmlMimeMail.php on line 468

    thanks for advance!
    AndreaB.

  • any returns where there is logic, for instance:

    return $this->auth ? $this->ehlo() : $this->helo();

    Try changing it so it returns a variable you setup:

    $return = $this->auth ? $this->ehlo() : $this->helo();
    return $return;

    This should stem at least some of your problems with the warnings, however I'm finding it difficult to read your comment and compare the code to the warning's you have included.

  • palash

    This code is buggin me public function &execute_query($buffered = true, &$link)
    {
    $this->connection_recent = $link;

    $this->querycount++;
    if ($queryresult = $this->functions[($buffered ? "query" : "query_unbuffered")]($this->sql, $link))
    {
    $this->sql = "";
    return $queryresult;
    return $return;
    }

    $this->sql = "";
    }

    same error. but it's indicating the last ) , what should i do?

  • It appears you dont have the PHP version required, it is valid in PHP 4.0.4 and up to 5.4.0 where they removed it, try changing &$link to just $link. Should work fine!

  • Adi Dobrescu

    @palash

    Try to return something from the else branch of the if statement. Eg.:
    if(....)
    {
    ....
    return $queryresult;
    }
    ......
    $this->sql="";
    return $null;
    }