Help - Search - Member List - Calendar
Full Version: trying to pass by reference to an optional function parameter
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
John T
I am trying to make a function that takes an optional parameter that gets
passed by reference.

Here is the first line of my function definition:

function funQueryDatabase($strQuery, &$intInsertId = NULL) {

I am getting this error:

Parse error: parse error, expecting `')'' in c:program
fileseasyphp1-8wwwmy_query_database_function.php on line 7

Line 7 is the first line of my function definition (above).

If I take out the & or if I take out the = NULL, then the error goes away,
but of course it doesn't do what I want.

Is it not possible to have an optional pass-by-reference parameter, or is
there another value that I should use for the default value?

Thanks very much for any help you can give.

JT
[Email Removed]__nospam

Kimmo Laine
"John T" <[Email Removed]_nospam> wrote in message
news:Licpe.588$%[Email Removed]...
QUOTE
I am trying to make a function that takes an optional parameter that gets
passed by reference.

Here is the first line of my function definition:

function funQueryDatabase($strQuery, &$intInsertId = NULL) {

I am getting this error:

Parse error: parse error, expecting `')'' in c:program
fileseasyphp1-8wwwmy_query_database_function.php on line 7

Line 7 is the first line of my function definition (above).

If I take out the & or if I take out the = NULL, then the error goes away,
but of course it doesn't do what I want.

Is it not possible to have an optional pass-by-reference parameter, or is
there another value that I should use for the default value?


This has been disuceesd in The Manual's user comments. Here's a post on that
topic by "bishop" quoted from
http://fi.php.net/manual/en/functions.arguments.php (wouldn't hurt to take a
look at the entire page)

"Functions explicitly prototyped with formal parameters passed by reference
can't have default values. However, functions prototyped to assign default
values to formal parameters may be passed references.

For example, this is a parse error:

function foo(&$bar = null) {
// formal parameters as references can't have default values
$bar = 242;
}

While this is perfectly legal (and probably what you want, mostly):

function foo($bar = null) {
$bar = 242;
}

foo(); // valid call, no warnings about missing args
foo(&$x); // valid call, post $x == 242
"

And this has been asked in here also earlier...

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com

John T
Thanks for pointing that out. However, I still get a warning when I do it
that way.

How about passing an array parameter, then assigning a default value to the
array? I know some languages automatically pass all arrays as references.
Does PHP do this? I attempted this, but it didn't work, but maybe I'm doing
it wrong.

Thanks,
JT

"Kimmo Laine" <[Email Removed]> wrote in message
news:Nifpe.3326$[Email Removed]...
QUOTE
"John T" <[Email Removed]_nospam> wrote in message
news:Licpe.588$%[Email Removed]...
I am trying to make a function that takes an optional parameter that gets
passed by reference.

Here is the first line of my function definition:

function funQueryDatabase($strQuery, &$intInsertId = NULL) {

I am getting this error:

Parse error: parse error, expecting `')'' in c:program
fileseasyphp1-8wwwmy_query_database_function.php on line 7

Line 7 is the first line of my function definition (above).

If I take out the & or if I take out the = NULL, then the error goes
away,
but of course it doesn't do what I want.

Is it not possible to have an optional pass-by-reference parameter, or
is
there another value that I should use for the default value?


This has been disuceesd in The Manual's user comments. Here's a post on
that
topic by "bishop" quoted from
http://fi.php.net/manual/en/functions.arguments.php (wouldn't hurt to take
a
look at the entire page)

"Functions explicitly prototyped with formal parameters passed by
reference
can't have default values. However, functions prototyped to assign default
values to formal parameters may be passed references.

For example, this is a parse error:

function foo(&$bar = null) {
// formal parameters as references can't have default values
$bar = 242;
}

While this is perfectly legal (and probably what you want, mostly):

function foo($bar = null) {
$bar = 242;
}

foo();    // valid call, no warnings about missing args
foo(&$x); // valid call, post $x == 242
"

And this has been asked in here also earlier...

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com



Xenophaw
"John T" <[Email Removed]_nospam> wrote:
QUOTE
Thanks for pointing that out.  However, I still get a warning when I do it
that way.

How about passing an array parameter, then assigning a default value to
the
array?  I know some languages automatically pass all arrays as references.
Does PHP do this?  I attempted this, but it didn't work, but maybe I'm
doing
it wrong.

Thanks,
JT

"Kimmo Laine" <[Email Removed]> wrote in message
news:Nifpe.3326$[Email Removed]...
"John T" <[Email Removed]_nospam> wrote in message
news:Licpe.588$%[Email Removed]...
I am trying to make a function that takes an optional parameter that
gets
passed by reference.

Here is the first line of my function definition:

function funQueryDatabase($strQuery, &$intInsertId = NULL) {

I am getting this error:

Parse error: parse error, expecting `')'' in c:program
fileseasyphp1-8wwwmy_query_database_function.php on line 7

Line 7 is the first line of my function definition (above).

If I take out the & or if I take out the = NULL, then the error goes
away,
but of course it doesn't do what I want.

Is it not possible to have an optional pass-by-reference parameter, or
is
there another value that I should use for the default value?


This has been disuceesd in The Manual's user comments. Here's a post on
that
topic by "bishop" quoted from
http://fi.php.net/manual/en/functions.arguments.php (wouldn't hurt to
take
a
look at the entire page)

"Functions explicitly prototyped with formal parameters passed by
reference
can't have default values. However, functions prototyped to assign
default
values to formal parameters may be passed references.

For example, this is a parse error:

function foo(&$bar = null) {
// formal parameters as references can't have default values
$bar = 242;
}

While this is perfectly legal (and probably what you want, mostly):

function foo($bar = null) {
$bar = 242;
}

foo();    // valid call, no warnings about missing args
foo(&$x); // valid call, post $x == 242
"

And this has been asked in here also earlier...

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com





PHP doesn't pass arrays or objects by reference for default (well, at least
not until PHP 5, if I remember well). If you want to use an optional
argument, you can use the func_get_arg() passing the argument number you
want (0 for the first). In that case the function would get:

function funQueryDatabase($strQuery /*, &$intInsertId*/) {
/*...*/
if ((func_num_args() > 1) {
$id =& func_get_arg(1);
/*...*/
}
/*...*/
}

And you could call it with funQueryDatabase($str, 10) or
funQueryDatabase($str, &$id).
In this way you avoid the need to pass a variable name for the second
parameter (required if the formal parameter is in the form &$id, which would
not permit to use a number or a string).


I hope this answers to your question.
--
Xenophaw


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.