Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4567510
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:56:27+00:00 2026-05-21T18:56:27+00:00

Firstly, I want to say I’m not trying to start a war here. I’m

  • 0

Firstly, I want to say I’m not trying to start a war here. I’m aware there can be some strongly held opinions from my own conversations on the topic that follows so I am looking for an answer that outlines the pros and cons.

In loosely typed languages (and I’m specifically coming from a PHP standpoint), there are two ways, in design terms, to pass parameters (note, I’m not talking about getting the parameters within the called functions).

On the one hand you can pass them as individual parameters as below:

foo($var1, $var2, $var3 ...);

On the other hand you can package up your variables into an array and then pass a single argument to the function, the array:

 $bar[] = $var1;
 $bar[] = $var2;
 $bar[] = $var3;
 ...

 foo($bar);

I can see the benefit of the second method when you have a lot of variables that need to be handled in a generic way, however, it also hides what variables the function needs in order to operate correctly.

The first solution, while it quickly becomes a long function call as the number of variables increases, better shows what variables the function requires and enables leveraging functionality to assign a default value if a null value is passed in the function call.

So, under what circumstances should one or the other be used and what about the middle ground, where for example, the array is associative so you can get the variables out by name inside the function and use them in a non-generic way? To me the associative array scenario seems very un-optimal and is simply a way that some developers use to make the function call itself shorter while actually increasing the overall amount of code written.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:56:28+00:00Added an answer on May 21, 2026 at 6:56 pm

    I think that your example of passing an array is pretty pointless. If you do this you should definitely pass the arguments using the normal way.

    Passing an array is only useful if a function accepts several optional parameters which could be passed in pretty much any order:

    function x($width = 100, $height = 100, $color = 'red')
    

    Here, in case you want to change the color, but not change the default width and height you would need to do:

    x(100, 100, 'blue');
    

    I.e. you would need to copy the default values in your code.

    Instead one could declare the function as follows:

    function x(array $opts) {
        $opts += array('width' => 100, 'height' => 100, 'color' => 'red');
    }
    x(array('color' => 'blue'));
    

    For a more real-life example, have a look at the constructor of Twig_Environment: As you can see, there are quite some options, most of them you will probably want to be kept at their default value. If you had to pass all of this options as an argument, you would just bloat the code by copying default values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly I just want to say I'm not an ASP developer, I'm PHP through
Let's say I have some email: some_email@yahoo.com . Firstly I would want to remove
Firstly, I want to apologize for my English, not my strongest side. To the
I've got problem with DirectX in C#. I want to draw some lines. Firstly
I need to download a json file from web. Firstly I want to say
Firstly I want to say I know little english. I have seeking about this
I need help, any ideas? Firstly i want to hide some input element when
Firstly, I do not want an alternative code, I only want help to make
I'm new here so I firstly like to say hello. My problem is that
Firstly, I will say what I want to compare the following: My Custom Object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.