General developer forum

=& - Simple question

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
Re: =& - Simple question
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers
Joseph, I've not looked up the functions you've mentioned above to see them in context. However, as a general rule, they may still be valid, if the parameters are not objects.

If you want to pass an array into a function and have it filled in, or you want to pass in a variable to be filled with a string or integer, then passing by reference is valid (in the function definition, but not at call time). If you are passing an object in to a function, or taking a reference to an object to update it (eg inside a form definition), you should not include the & symbol as PHP5 already passes objects as references.
Average of ratings: Useful (1)
Tim at Lone Pine Koala Sanctuary
Re: =& - Simple question
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

As Davo says, most, but not all, &s should go, but it is not critcial.

I just wanted to point out that instead of a function like

function return_two_things_nasty($input, &$otherreturnvalue) {
    $otherreturnvalue = 'two ' . $input;
    return 'one ' . $input;

$otherreturnvalue = '';
$firstreturnvalue = return_two_things_nasty('sheep', $otherreturnvalue);

you can instead do

function return_two_things_nice($input) {
    return array('one ' . $input, 'two ' . $input);

list($firstreturnvalue, $otherreturnvalue) = return_two_things_nice('sheep');

That gets rid of one of the reasons you might otherwise pass-by-reference.

Average of ratings: Useful (2)