5

Due to inconsistencies in the PHP manual (as I've posted about before) I'm just inquiring about some clarification.

The Function Arguments page (http://ca2.php.net/manual/en/functions.arguments.php) has the following note:

Note: As of PHP 5, default values may be passed by reference.

Now, I assume this simply means that the following syntax is acceptable:

function foo(&$bar = null){
    // ...
}

However, again due to other inconsistencies, I was wondering if perhaps this pertains to something else.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
  • Hmm, apparently you can set a default to a variable passed by reference in PHP. I can't think of a case where that wouldn't be a sign you're doing something wrong though. – Tim Gautier Sep 07 '11 at 19:37
  • 1
    Thanks @Tim - I've been using it in a double-duty function to resolve/validate request routes. When resolving, an additional argument is passed by reference, and is populated with resolved parameters. When validating, no argument is necessary as you only need the boolean return value. This is not unlike `preg_match`. – Dan Lugg Sep 07 '11 at 19:42

3 Answers3

4

It means that in PHP 4, using a default value for arguments passed by reference would result in a parse error:

Parse error: syntax error, unexpected '=', expecting ')' in ...

Demo

In PHP5, when no argument is passed, your function will have a normal local variable called $bar initialized to null.

It should probably be reworded to:

Note: As of PHP 5, function declarations may define a default value for argument passed by reference.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • I figured this much; the wording *is* mildly confusing -- it sort of leads you to believe there's more than meets the eye. Agree on the rewording; mentioned that myself: http://chat.stackoverflow.com/transcript/message/15381785#15381785 – Dan Lugg Mar 20 '14 at 06:44
  • The wording makes it seem that you could somehow do `function foo($bar = &$something)` which is kind of absurd ;-) – Ja͢ck Mar 20 '14 at 07:18
4

it means that when you change bar

$bar = "newvalue";

in function, old (original one) will be affected too

<?php
function foo(&$bar = null){
    $bar = 'newval';
}

$bar = 'oldval, will be changed';
foo($bar);
echo $bar; //RETURNS newval

so if you change any variable passed by reference, it doesn't matter where you changed, source one is changed, too

http://sandbox.phpcode.eu/g/51723

genesis
  • 50,477
  • 20
  • 96
  • 125
  • Thanks @genesis - I understand that's the intended functionality of references in general. The question more pertained to the nondescript mention of reference arguments and defaults. As I said, I assume simply that it means arguments accepted by reference can also have default values; I was wondering if it referred perhaps to some other (*otherwise unmentioned perhaps*) feature or side-effect. – Dan Lugg Sep 07 '11 at 19:16
  • Neither do I, but the manual has surprised me before. The manual goes into reasonable detail on that page alone (*and other sections; "References Explained", etc.*) about passing references by argument. It seems like that caveat is either unnecessary, or pertains to something else that I'm not seeing. You know what I mean? – Dan Lugg Sep 07 '11 at 19:20
  • @Bracketworks: problably yes. I think you should be afraid about these. If you have some problems with these, you can always open question here – genesis Sep 07 '11 at 19:25
0

I think the only reason this exist is for allowing to skip trailing parameters in a function call

function test(&$bar = 10)
{
    echo " '$bar' ";
    $bar = $bar*2;
    echo " '$bar' ";
}

test($aaa);     // prints '' '0'     (NULL as string, NULL*2)
echo $aaa;      // prints 0          ($aaa is set to NULL*2)
echo "<br>";

$bbb = 6;
test($bbb);     // prints '6' '12'   (6, 6*2)
echo $bbb;      // prints 12         ($bbb is set to 6*2)
echo "<br>";

test();        // prints '10' '20'
// (uses the default value since the argument was skipped, so: 10, 10*2)

So imho the reason this exist is merely the possibility to have $bar set to some default value inside the function scope when you skip the leading parameter in the function's call

If it's like so I agree with you, the manual should be more precise about this