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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:18:26+00:00 2026-06-10T07:18:26+00:00

I don’t have a specific situation I’m dealing with, so consider this more of

  • 0

I don’t have a specific situation I’m dealing with, so consider this more of a general question. If language is important, answers can tend more toward PHP.

If I have a large array (or other data type) of data which I require in multiple functions, that does not change, how should I use that data? For example, maybe I have an array that looks like

$states = array(
                "AL" => "Alabama",
                     ...
                "WY" => "Wyoming"
                );

Obviously, this array won’t need to change anytime soon. If I have five functions that have to use the above array, what is the best solution? My three ideas are:

  • Declare it as a global constant

This one seems the most straightforward, however some people I’ve talked to seem to recommend shying away from using global variables. Any insight here would be appreciated.

  • Pass it into the function

This one seems like a bad idea to me because it never changes, and function parameters should be for variables, right? Not to mention having to pass it into a function that doesn’t use it, just so it can be used by another function. That seems like a poor practice.

  • Have it be returned by a function

This is one that I haven’t seen used a lot, but I’ve used myself a couple of times for passing mysqli_connect() information to multiple functions. It worked quite well. Is this regarded as poor practice? Should I just suck it up and use a global constant?

I realize that using a global constant sounds obvious, but I’ve heard (and read) more than one mantra like, “if you’re declaring globals, you’re doing it wrong” and stuff like that. Can anyone explain why this might be?

Thanks for any insight that you can give me, guys.

  • 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-10T07:18:27+00:00Added an answer on June 10, 2026 at 7:18 am

    My general advice would be to build a “service provider” class, which is analogous to your “return it from a function” option. Here are some specific thoughts:

    • Declare it as a global constant

    First, constants can only be scalar values, so you cannot technically have an “constant” array. Second, what you say is almost always true: globals are a hint that there is a better way (with some exception).

    • Pass it into the function

    If they are actually constant (in the “real world” sense, not the computer-science sense), then you are right, passing to a function should be considered code-clutter (unless the functions should be abstracted from the knowledge of the “global” value, but that’s a domain-specific architecture choice.

    • Have it be returned by a function

    Ding Ding! The reason is essentially this: Anyone can access these to “read”, but only the “function” can “write”. I put “function” in quotes because it could also be a class or singleton object instance. Basically, you provide a semantically appropriate provider of this static information.

    One of the benefits of this approach is that you may only need the data quite seldom in a long-running program. The service provider may in that case be written to fetch the values from some out-of-memory persistence and free the memory when it is not needed anymore. Nobody else should need to be responsible for that memory management. Along the same lines, imagine that the project grows and these datum need to be loaded from a database instead of a static array… if you have centralized the access, this is remarkably easy to facilitate and you don’t need to have a huge heap of memory lying around for the entire length of your program’s execution holding values that are almost never accessed.

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

Sidebar

Related Questions

Don't know why but I can't find a solution to this. I have 3
Don't ask me how but I'm in a situation where I have DCPs published
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't know if this has been asked before, so point me to another question
Don't know if anyone can help me with this or if it's even possible.
I don't know if this question is trivial or not. But after a couple
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't think my virtualhost is working correctly. This is what I have inside of

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.