Results 1 to 8 of 8

Thread: Pass a function a variable number of arguments by reference

  1. #1
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Pass a function a variable number of arguments by reference

    So, is it possible to pass a function a variable number of arguments by reference? Everything I have found says no. One page provided a couple of hackish solutions. This is the function I'm working on by the way. I'm using an array to make it possible, but I would rather not.
    PHP Code:
    function sqlite_escape_list(&$array){
        foreach(
    $array as $single){
            
    $single sqlite_escape_string($single);
        }


  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    seems like the logical way to do it.
    not much you can do to get around it.

    returning might be a bit complex, but you could put it back into an array or compute to a single value.

    this is how one could do a function for an average, for example.

    You could also do something similar with a CSV type list, etc., but it would just be more coding.
    Might be nice, though, if you wanted to call it like:
    csvfunc('1,2,3,4');


    The only other option is using up to a specified number of variables, like:
    function 3vars($a=0,$b=0,$c=0);
    That would allow for no value of 1 or 2 of them and it would use 0, meaning it might have no effect, depending on the math used.
    So... you could have "up to" X variables in it.


    last thought-- you could use something complex with the eval function to analyze how many varialbes were input and to write, then execute a function depending on that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by djr33
    returning might be a bit complex, but you could put it back into an array or compute to a single value.
    Yes, but the defeats the whole purpose of a referece. The idea of a reference is that when you update the reference the original is updated as well. I know how to pass a variable number of arguments in. What I don't know how to do is pass in a variable number of arguments that are references.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Hmm.... might you just want to repeat the function for each?

    The way to do that is the opposite...
    foreach{
    function();
    }

    as opposed to putting the foreach in the function.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    That defeats the purpose too, in this case. My function ideally should allow me to shorten
    PHP Code:
    $name sqlite_escape_string($name);
    $email sqlite_escape_string($email);
    $pass sqlite_escape_string($pass);
    $address sqlite_escape_string($address);
    //and so on 
    down to just
    PHP Code:
    sqlite_escape_list($name,$email,$pass,$address); 
    right now I have it down to
    PHP Code:
    sqlite_escape_list(array(&$name,&$email,&$pass,&$address)); 
    Last edited by blm126; 11-03-2006 at 12:15 PM.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Meh.

    There are many reason the first syntax won't work. Basically.... you must seperately use each variable from the funtion definition in the function.
    The way to make that work would be to set like 100 variables in the initial function and type it out manually for each. Then include an "if ([equals default) {[ignore]};" statement for each.
    That would allow up to 100.

    The only way to do what you're talking about, I think, is the CSV approach and/or using the eval function.
    Both would mean, in short, using the syntax of func('$1,$2,...'); and going from there.

    But.... arrays will too. why not use arrays?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Well, I also have this written.
    PHP Code:
    function sqlite_escape_list(){
        
    $array func_get_args();
        foreach(
    $array as $single){
            
    $single sqlite_escape_string($single);
        }

    The problem is that I can't figure out how to get that code to work with references.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah. Didn't know about those functions.
    That might work.

    This is pretty much over my head. What I've said so far is the best I can do....

    Good luck, and post the final code, if you get it to work
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •