Professional Web Applications Themes

trying to pass by reference to an optional function parameter - PHP Development

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: P error: p 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 ...

  1. #1

    Default 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:

    P error: p 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
    com__nospam




    John Guest

  2. #2

    Default Re: trying to pass by reference to an optional function parameter

    "John T" <net_nospam> wrote in message
    news:Licpe.588$%news.prodigy.com... 


    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 p 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


    Kimmo Guest

  3. #3

    Default Re: trying to pass by reference to an optional function parameter

    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" <com> wrote in message
    news:Nifpe.3326$news.jippii.net... [/ref]
    away, [/ref]
    is 
    >
    >
    > This has been disuceesd in The Manual's user comments. Here's a post on[/ref]
    that 

    reference 


    John Guest

  4. #4

    Default Re: trying to pass by reference to an optional function parameter


    "John T" <net_nospam> wrote: [/ref]
    > away, [/ref]
    > is 
    >>
    >>
    >> This has been disuceesd in The Manual's user comments. Here's a post on[/ref]
    > that 
    > a 
    > reference 
    >
    >[/ref]

    PHP doesn't pass arrays or objects by reference for default (well, at least
    not until PHP 5, if I remember well). If you want to use an optional
    argument, you can use the func_get_arg() passing the argument number you
    want (0 for the first). In that case the function would get:

    function funQueryDatabase($strQuery /*, &$intInsertId*/) {
    /*...*/
    if ((func_num_args() > 1) {
    $id =& func_get_arg(1);
    /*...*/
    }
    /*...*/
    }

    And you could call it with funQueryDatabase($str, 10) or
    funQueryDatabase($str, &$id).
    In this way you avoid the need to pass a variable name for the second
    parameter (required if the formal parameter is in the form &$id, which would
    not permit to use a number or a string).


    I hope this answers to your question.
    --
    Xenophaw


    Xenophaw Guest

Similar Threads

  1. Replies: 5
    Last Post: August 11th, 11:22 PM
  2. Replies: 5
    Last Post: March 20th, 04:11 PM
  3. Replies: 0
    Last Post: September 7th, 06:53 PM
  4. T-SQL Function optional parameter ???
    By Russell in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 15th, 09:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139