note 70212 deleted from language.references.pass by danbrown

From: Date: Thu, 28 Jan 2010 13:19:07 +0000
Subject: note 70212 deleted from language.references.pass by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165416@lists.php.net to get a copy of this message
Note Submitter: Train Boy ---- Be nice to put the argument about whether reference parameters can have default values or not to rest. First, a little reminder from the section on function arguments, "The default value must be a constant expression, not (for example) a variable, a class member or a function call". This means that you can't assign a default value that references another variable, to the argument, but you should be able to assign a constant such as NULL. Having said that, the following code works pretty much as expected in both PHP 5.1.4 and 5.1.2: function foo($Inv, &$Ptr=NULL) { echo "In foo, Inv = ".$Inv.", Ptr = ".$Ptr."<br>\n"; if (isset($Ptr)) $Ptr = "set"; } $Val = "unset"; foo(1); echo "Val = ".$Val."<br>\n"; // outputs "unset" foo(2, $Val); echo "Val = ".$Val."<br>\n"; // outputs "set" However PHP 4.2.2 is a whole 'nother story. The function prototype gets a parse error right at the equal sign. So it would appear that both camps are right, depending on which version of PHP you're using. If you can control which one you run under and can pick one the V5 versions, it looks like you're in good shape. If you have one of the V4 versions, you're probably not a happy camper.

« previous php.notes (#165416) next »