23

What does the & before the function name signify?

Does that mean that the $result is returned by reference rather than by value? If yes then is it correct? As I remember you cannot return a reference to a local variable as it vanishes once the function exits.

function &query($sql) { // ... $result = mysql_query($sql); return $result; } 

Also where does such a syntax get used in practice ?

4 Answers 4

9

Does that mean that the $result is returned by reference rather than by value?

Yes.

Also where does such a syntax get used in practice ?

This is more prevalent in PHP 4 scripts where objects were passed around by value by default.

Sign up to request clarification or add additional context in comments.

12 Comments

-1 for stating $result would only exist within the function and would therefore be useless.
@nikic: do you have any support for your claim that objects in php5 are passed by value?
@nikic: I don't think that proves that objects are passed by value. If you set a local variable to a new value, it's not the same object. You'd have to write a function that modifies an object of some mutable type and show that the value passed in was not changed externally to prove that the parameter was passed by value (copied).
@nikic: your example is a bit convoluted since you are intermingling object and string datatypes. a correct example would be: $a = new stdClass; $a->prop='foo'; function test($b){ $b->prop = 'bar'; } test($a); echo $a->prop; As you can see, the object is passed by-ref :)
What PHP calls "objects" are always passed by value unless otherwise noted. The difference between PHP4 and PHP5 is that in PHP5 you pass around references to the objects.
|
7

To answer the second part of your question, here a place there I had to use it: Magic getters!

class FooBar { private $properties = array(); public function &__get($name) { return $this->properties[$name]; } public function __set($name, $value) { $this->properties[$name] = $value; } } 

If I hadn't used & there, this wouldn't be possible:

$foobar = new FooBar; $foobar->subArray = array(); $foobar->subArray['FooBar'] = 'Hallo World!'; 

Instead PHP would thrown an error saying something like 'cannot indirectly modify overloaded property'.

Okay, this is probably only a hack to get round some maldesign in PHP, but it's still useful.

But honestly, I can't think right now of another example. But I bet there are some rare use cases...

Comments

7

Does that mean that the $result is returned by reference rather than by value?

No. The difference is that it can be returned by reference. For instance:

<?php function &a(&$c) { return $c; } $c = 1; $d = a($c); $d++; echo $c; //echoes 1, not 2! 

To return by reference you'd have to do:

<?php function &a(&$c) { return $c; } $c = 1; $d = &a($c); $d++; echo $c; //echoes 2 

Also where does such a syntax get used in practice ?

In practice, you use whenever you want the caller of your function to manipulate data that is owned by the callee without telling him. This is rarely used because it's a violation of encapsulation – you could set the returned reference to any value you want; the callee won't be able to validate it.

nikic gives a great example of when this is used in practice.

1 Comment

If the function is not defined with a & before the name of the function, can you still return the variable with reference instead of value?
3
 <?php // You may have wondered how a PHP function defined as below behaves: function &config_byref() { static $var = "hello"; return $var; } // the value we get is "hello" $byref_initial = config_byref(); // let's change the value $byref_initial = "world"; // Let's get the value again and see echo "Byref, new value: " . config_byref() . "\n"; // We still get "hello" // However, let’s make a small change: // We’ve added an ampersand to the function call as well. In this case, the function returns "world", which is the new value. // the value we get is "hello" $byref_initial = &config_byref(); // let's change the value $byref_initial = "world"; // Let's get the value again and see echo "Byref, new value: " . config_byref() . "\n"; // We now get "world" // If you define the function without the ampersand, like follows: // function config_byref() // { // static $var = "hello"; // return $var; // } // Then both the test cases that we had previously would return "hello", regardless of whether you put ampersand in the function call or not. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.