NYCPHP Meetup

NYPHP.org

[nycphp-talk] passing optional arguments by reference

David Sklar sklar at sklar.com
Fri Jan 16 14:15:54 EST 2004


>> function &execute($stmt, &$data = '' ) {
>
> That produces a parse error too.

It makes sense that you can't have a optional pass-by-reference argument:
what is supposed to happen in this code?:

----
function &execute($stmt, &$data = '') {
   global $dbh;
   $data = $dbh->query($stmt);
   if (PEAR::isError($data)) {
       return false;
   } else {
      return true;
   }
}

// ... instantiate DB connection in $dbh ...

$retval = execute('SELECT * FROM foo');
----


If the second argument is optional and not provided, there's nothing to
attach $data to inside the function and the value is tossed away. It seems
reasonable that when a function says it expects a reference to a variable,
that reference must be present for the function to work properly.

David







More information about the talk mailing list