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 238811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:32:07+00:00 2026-05-11T20:32:07+00:00

Occasionally I’ll write a PHP function with a single input, an associative array containing

  • 0

Occasionally I’ll write a PHP function with a single input, an associative array containing all of that function’s inputs. This has benefits such as not having to remember the correct order of inputs, but I’ve also noticed it makes implementing changes to large codebases much easier; when I need to add another variable, and that variable has to pass through 4 or 5 pre-existing functions, it’s much easier when I can just stick it in the array and pass it along.

My question is, is there a downside to doing this?

I rarely see functions written this way in examples, or in open source I use, which leads me to believe there is probably a downside. If there isn’t a reason not to do it then why not write all functions this way?

UPDATE

Thanks for all your answers. It looks like two major problems stand out:

Code readability – impossible to tell what variables are going into a function and what they’re for

Variable creep – Could wind up with massive arrays bouncing from one function to the next; one should not pass parameters to functions that don’t require them

Which are both great points I did not think about.

It seems the general gist as well is that code where this is an issue, should probably be converted to a class. Unfortunately in this specific project such a refactoring is beyond the scope, but I also think Bill Karwin’s solution is good – pass an array of optional variables

  • 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-11T20:32:07+00:00Added an answer on May 11, 2026 at 8:32 pm

    why not write all functions this way?

    For that matter, why not forget about parameters completely, and use global variables for everything? (kidding)

    Passing an associative array has one useful advantage: you can make multiple function parameters optional, and you can pass a value for the Nth parameter without having to pass a value for the *N-1*th parameter.

    But you have no way to make mandatory parameters with a compile-time error if you don’t pass them. Neither can you declare type-checking.

    You’ll have to write code inside the called function to check for the presence and the type of required parameters.

    An alternative I have used is to declare conventional parameters for those that are mandatory, and then as the last (optional) argument, declare an associative array called $options that contains only the optional items.

    function database_connect($dbname, $user, $password, array $options = array())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 129k
  • Answers 129k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Subversion has post commit hooks which allow you to perform… May 12, 2026 at 5:57 am
  • Editorial Team
    Editorial Team added an answer > print(string.format("%18.0f",1247687475123456)) 1247687475123456 May 12, 2026 at 5:57 am
  • Editorial Team
    Editorial Team added an answer Finally, I found the answer here is the code snippet… May 12, 2026 at 5:57 am

Related Questions

Occasionally I come accross a unit test that doesn't Assert anything. The particular example
Occasionally I come across methods with an uncomfortable number of parameters. More often than
Occasionally I search for some JavaScript help and I come upon the term Server-side
Occasionally I have to do some profiling work on Java code, and I would
Occasionally I need to use fixed-width integers for communication with external devices like PLCs.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.