468,765 Members | 860 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

[HELP] trying to pass by reference to an optional function parameter

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
jo********@yahoo.com__nospam
Jul 17 '05 #1
5 2299
I have used this type of passing the variable by reference in the
optional parameter. But it was in PHP 5. When I tried to use the same
function in some other project, in the PHP 4.3.9, I got the same error
message, but in PHP 5, it was working perfectly. So, I guess, it is not
for PHP 4. Not sure.

Also I am looking forward to get the reason and the solution for it.

Jul 17 '05 #2
You can't do it in PHP 4. It doesn't make a whole lot of sense to have
a reference to nothing.

Just return the value.

Jul 17 '05 #3
> Just return the value.
I am already using the return for a different value -- the one that always
gets returned -- so I need a different method to get the optional value out.

The PHP manual has a message saying you can pass a reference in the call to
the function
e.g.,
function foo($bar = null) {
$bar = 242;
}
foo(&$x); // $x == 242

but that still gives me a warning.

How about an 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.

Unfortunately, my web host is using PHP 4.3.2, not PHP 5.
It doesn't make a whole lot of sense to have a reference to nothing. It's not the nothing that it's objecting to, it is not legal to have any
default value for a parameter passed by reference.

Thanks,
JT

<ch***********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com... You can't do it in PHP 4. It doesn't make a whole lot of sense to have
a reference to nothing.

Just return the value.

Jul 17 '05 #4
John T said the following on 10/06/2005 21:52:
It doesn't make a whole lot of sense to have a reference to nothing.


It's not the nothing that it's objecting to, it is not legal to have any
default value for a parameter passed by reference.


Yes, by definition passing a reference means that you're passing a
pointer to an existing variable. If that variable doesn't exist, there
is nothing to set to NULL if you do something like:

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

So by definition this doesn't make sense.

--
Oli
Jul 17 '05 #5
Return the two values in an array, then use list() to separate them
out.

function a() {
...
return array($var1, $var2);
}

list($ret1, $ret2) = a()

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by intrepid_dw | last post: by
3 posts views Thread by GS | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.