views:

56

answers:

3
A: 

I believe it's referring to byref arguments not functions. For example this:

function doStuff(&$value1, &$value2) {
    ...
}

is an acceptable use of byref because the doStuff() function has to return 2 values. If it only doStuff() only needed to affect one value, it would be more elegant to have the function return it, by value, of course.

Asaph
A: 

The bolded part means it's useful if you want to keep a reference to a variable, instead of the value of this variable.

The example about returning references, on php.net, explains it pretty well, IMO.

Nicolas
+2  A: 

Na. You can't pass a reference to a function name. When passing a variable by reference, if you change it's value in your function, it's value will also be changed outside of the function.

For example :

function test(&$var) {
    $var = strtolower($var);
}

function second_test($var) {
    $var = strtolower($var);
}

$var = 'PHP';

second_test($var);
echo $var;

echo "\r\n";

test($var);
echo $var;

This will display :

PHP
php

As the second_test method doesn't have the variable passed by reference, it's updated value is only updated inside the function. But the test method as the variable passed by reference. So it's value will be updated inside and outside of this function.

Damien MATHIEU
@dmathieu You can in fact use byref in front of a function. It's perfectly legal PHP. Although since the introduction of PHP5, it's been discouraged.
Asaph
Well providing a deprecated technique isn't what I'd call a good advice ;)
Damien MATHIEU
It's not deprecated in any way. Also, its use is only discouraged in situations where an object is being returned by reference, because in PHP5 it's done by default.
Ignas R
It was used in PHP 4 as a hack to work around shortcomings of the engine. In PHP 5 this isn't needed. That leaves very few (if any) legal cases for using it, but it's not deprecated per se.
troelskn
@all, thanks for your replies. but i still want to ask: if i am not returning a class variable, in this case just a function variable, am i right to say i shld not use this (return by reference) because its already done by the engine?
iceangel89
The variables by reference should be used to return multiple variables in one single function.So if you have only one variable to return, you don't need them.
Damien MATHIEU
but in the example shown http://us2.php.net/manual/en/language.references.return.php, it returns only 1 variable
iceangel89