Mohamed Omar
Mohamed Omar

Reputation: 473

Why not to use return($var) if referencing the variable?

As PHP manual states

Note: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you're not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a).

I can not understand why not while the following code examples will give the same result.

The code with return $var:

<?php
  function a(&$a) {
  $a .= "c";
  return $a;
  }
 $b = "b";
 echo a($b);
 echo $b;

 ?>

The code with return ($var):

<?php
  function a(&$a) {
  $a .= "c";
  return ($a);
  }
 $b = "b";
 echo a($b);
 echo $b;

 ?>

Upvotes: 0

Views: 55

Answers (4)

AbraCadaver
AbraCadaver

Reputation: 78994

The examples you show are Passing by Reference, where you pass a reference of a variable to a function. The quote from the manual is about Returning References of a variable in a function.

Just like you can't pass an expression by reference, you can't return an expression by reference, and wrapping a variable in () turns it in to an expression.

Passing a Reference

function a(&$b) { $b = 1; }

$x = 0;
a($x);
echo $x; // echos 1, because a reference to $x was changed

However a(abs($x)); or even a( ($x) ); generates:

Strict Standards: Only variables should be passed by reference

Returning a Reference

class a {
    public $c = 0;
    public function &b() { return $this->c; }
}

$a = new a;
$x = &$a->b();
$a->c = 1;
echo $x; // echos 1, because $x is a reference to $a->c that was changed

However, return ( $this->c ); generates:

Notice: Only variable references should be returned by reference

Upvotes: 1

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

The example you give is not about returning references, but is an example of passing references.

function myfunc(&$arg) {
   // here $arg has been passed by reference, nothing to do with the docs you quoted
}

The docs are about this:

function & myfunc($arg) {
    // here you create your $result using $arg and whatever
    return $result; // this will work
    return ($result); // this will NOT
}

// and how you use it
$res =& myfunc(1);

Upvotes: 1

Roman
Roman

Reputation: 394

When you pass any value to the function, php copy the value and return a copy, not the variable you passed to the function. So if you want to change value and don't want to return anything from the function you need to declare, functions argument as reference - it means that any variable that you will pass to the function wont be copied by php and manipulation inside the function will change variable outside the function, for example:

$var = 1;

//not reference function
function notReference($argument)
{
    $argument++;
}

notReference($var);

echo $var; // you will get 1

function reference(&$argument)
{ 
    $argument++;
}

reference($var)

echo $var; // you will get 2,

Upvotes: 0

Jaime
Jaime

Reputation: 1410

You're modifying the variable, because it's passed by reference. But then you're setting it to the value that's returned by the function. That's why you're getting the same result.

When modifying a variable by reference, you don't need to return it. Your function will still have the same result if you write it like this:

function a(&$a) {
  $a .= "c";
}

Upvotes: 0

Related Questions