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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John T

    #1

    [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 funQueryDatabas e($strQuery, &$intInsertI d = NULL) {

    I am getting this error:

    Parse error: parse error, expecting `')'' in c:\program
    files\easyphp1-8\www\my_query_ database_functi on.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@yaho o.com__nospam


  • yehaimanish@gmail.com

    #2
    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.

    Comment

    • Chung Leong

      #3
      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.

      Comment

      • John T

        #4
        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.goo glegroups.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]


        Comment

        • Oli Filth

          #5
          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 funQueryDatabas e($strQuery, &$intInsertI d = NULL)

          So by definition this doesn't make sense.

          --
          Oli

          Comment

          • Chung Leong

            #6
            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()

            Comment

            Working...