Connecting Tech Pros Worldwide Help | Site Map

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

John T
Guest
 
Posts: n/a
#1: Jul 17 '05
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


yehaimanish@gmail.com
Guest
 
Posts: n/a
#2: Jul 17 '05

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


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.

Chung Leong
Guest
 
Posts: n/a
#3: Jul 17 '05

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


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.

John T
Guest
 
Posts: n/a
#4: Jul 17 '05

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


> 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.
[color=blue]
> It doesn't make a whole lot of sense to have a reference to nothing.[/color]
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

<chernyshevsky@hotmail.com> wrote in message
news:1118150775.633971.274280@f14g2000cwb.googlegr oups.com...[color=blue]
> 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.
>
>[/color]


Oli Filth
Guest
 
Posts: n/a
#5: Jul 17 '05

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


John T said the following on 10/06/2005 21:52:[color=blue]
>[color=green]
>>It doesn't make a whole lot of sense to have a reference to nothing.[/color]
>
> 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.
>[/color]

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
Chung Leong
Guest
 
Posts: n/a
#6: Jul 17 '05

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


Return the two values in an array, then use list() to separate them
out.

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

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

Closed Thread