Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:30 PM
John T
Guest
 
Posts: n/a
Default [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
johntutton@yahoo.com__nospam



  #2  
Old July 17th, 2005, 01:30 PM
yehaimanish@gmail.com
Guest
 
Posts: n/a
Default Re: 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.

  #3  
Old July 17th, 2005, 01:30 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: 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.

  #4  
Old July 17th, 2005, 01:31 PM
John T
Guest
 
Posts: n/a
Default Re: 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]


  #5  
Old July 17th, 2005, 01:31 PM
Oli Filth
Guest
 
Posts: n/a
Default Re: 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
  #6  
Old July 17th, 2005, 01:31 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: 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()

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.