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

  • Home
  • SEARCH
  • 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 7776215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:49:28+00:00 2026-06-01T17:49:28+00:00

Is it better in PHP to pass the variables as an array or as

  • 0

Is it better in PHP to pass the variables as an array or as a separate variable for each,

I mean in instances where out of a 30 associative array I probably use 5 of the variables out of it? Would it be better to pass them in separately as five variables, or just pass the whole array over?

  • 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-06-01T17:49:30+00:00Added an answer on June 1, 2026 at 5:49 pm

    In generic terms, there are some things to consider when it comes to choosing how to define your functions. This may not directly answer your question, but it may provide a rationale for choosing one way over another, and why they may be better for different things.

    function do_something($a, $b, $c, $d, $e) {
        ...
    }
    
    function do_something(array $a) {
        ...
    }
    

    First and foremost, there are the semantics. Does your function depend on that particular array, or is it just that the arguments you would pass happen to be in an array in that case?

    If it is dependent on the array, then it would stand to reason to pass that in as the sole argument, and the code remains readable, because you wouldn’t use it in any other case:

    function do_something_to_big_array($big_array) {
        ....
    }
    

    If it isn’t that specific, you may have problems.

    The function becomes obscure:
    You can’t use it without reading the function to find out what elements the array must contain.

    The function becomes more complex:
    You can supply a default array with default values in the definition, but that won’t be used
    when you supply the argument. You’ll find yourself trying to re-implement a standard argument list, checking if keys exist, setting them if they don’t, etc.

    // default array if no argument supplied
    function do_something($a = array('a' => true, 'b' => 10, 'c' => false)) {
        // you don't know if you're working with the default or not
        if (!isset($a['c'])) $a['c'] = false;
        ...
    }
    
    // define a defaults array and replace duplicates with $a's values
    function do_something(array $a) {
        $defaults = array('a' => true, 'b' => 10, 'c' => false);
        $a = array_replace($defaults, $a);
    }
    
    // have defaults without extra logic in the function
    function do_something($a = true, $b = 10, $c = false) {
       ...
    }
    

    Imagine having to do that for every function you declare, so you can pass in an array and have it work reliably, instead of separate arguments.

    Readability suffers; more clutter:
    You have all the data you need to pass in to the function, but you have to create an associative array containing that data before you can use it.

    $data = array('a' => $a, 'b' => $b, 'c' => $c);
    do_something($data);
    

    Of course, there is always an exception, and you might find yourself with a large arguments list:

    function do_something($a, $b, $c, ... $x, $y, $z) {
       ...
    }
    

    That’s unreasonable and needs refactoring, so you can either pass in an array, and work with that, because you’re never going to use it in any other case:

    function do_something_with_alphabet(array $alphabet) {
        ...
    }
    

    Or maybe you could split it into more functions if it was reasonable to do so:

    function do_something_first($a, $b, $c) {
        ...
    }
    
    function do_something_last($x, $y, $z) {
        ...
    }
    

    There’s no point saying you have to do it this way, or that way, or even the other way, but it’s worth keeping it in mind because you may write a function in one situation, and then you (or someone else) may re-visit it many moons later, and have trouble figuring out what is going on. If it makes absolute sense to pass a single array to a function, then that is it. If it doesn’t, and it’s for convenience, then think about it a bit more.

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

Sidebar

Related Questions

Which is better to use in PHP, a 2D array or a class? I've
I'd like to be able to use php search an array (or better yet,
Everything I read about better PHP coding practices keeps saying don't use require_once because
I am trying to pass multiple variables in a URL in PHP to GET
I normally call perl scripts from PHP as below and pass in variables this
I need to pass a variable that php is aware of to my javascript
With regards to security and convenience which cookies are better the PHP ones or
I am working on a small project for learning PHP better. This project has
I've been battling PHP's email reading functions for the better part of two days.
What is the better way of getting the IP address in PHP: getenv('REMOTE_ADDR'); or,

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.