For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > June 2005 > trying to pass by reference to an optional function parameter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author trying to pass by reference to an optional function parameter
John T

2005-06-07, 8:55 am

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
files\easyphp1-8\www\my_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
johntutton@yahoo.com__nospam




Kimmo Laine

2005-06-07, 8:55 am

"John T" <johntutton@sbcglobal.net_nospam> wrote in message
news:Licpe.588$%j7.389@newssvr11.news.prodigy.com...
>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
> files\easyphp1-8\www\my_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

2005-06-10, 8:55 pm

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" <eternal.erectionN05P@Mgmail.com> wrote in message
news:Nifpe.3326$l24.1956@reader1.news.jippii.net...
> "John T" <johntutton@sbcglobal.net_nospam> wrote in message
> news:Licpe.588$%j7.389@newssvr11.news.prodigy.com...
away,[color=darkred]
is[color=darkred]
>
>
> 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
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com