Passing multiple parameters to a function in a variable

This topic contains 1 reply, has 2 voices, and was last updated by  Dave Wyatt 1 year, 9 months ago.

  • Author
    Posts
  • #32491

    ertuu85
    Participant

    Say i have a function

    function test($a, $b)
    {
    ...
    }
    
    And i have
    
    
    $x = "a b"
    
    I cant pass it
    
    > test $x 
    
    but i can pass it 
    
    > test a b
    
    Is there a way to pass multiple params through the one variable?
    
    ie 
    
    
    
    $x = "a b"
    
    test $x
  • #32492

    Dave Wyatt
    Moderator

    Yes, but not as a single string like that. Instead, you would have an array or a hashtable for your variable, and you would use a technique called Splatting. That looks like this:

    $array = 'a', 'b'
    test @array
    

    You can get all the gritty details in the about_Splatting help file. 🙂 https://technet.microsoft.com/en-us/library/jj672955.aspx

You must be logged in to reply to this topic.