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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:33:48+00:00 2026-06-08T18:33:48+00:00

I’m not so advanced with PHP and am looking for the best solution to

  • 0

I’m not so advanced with PHP and am looking for the best solution to the following:

  • I have a PHP array that holds important information about my script that

I need to RE-USE this frequently in my script.

$my_settings = array('width'=>'300', 'height'=>'200', ...);
  • I store that in a MySQL db.

  • The PHP script needs to get that array from MySQL once each time it runs in order to know how to compose the view.

(The array will eventually become quite large… Maybe few hundred rows…)

What is the best practice to get and define this array only ONCE in the beginning of my script, so I can re-use it all the time by other functions etc…?

Is it by defining it globally?

global $my_settings;
$my_settings = get_my_settings_from_db();

function returnSetting($key='') {
 global $my_settings;
 return $my_settings[$key];

}

Or is this bad idea?

What is the most efficient way to do it?

Thanks!

  • 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-08T18:33:49+00:00Added an answer on June 8, 2026 at 6:33 pm

    Instead of using global (which is a bad thing to do) you should inject the variable (or even better the part you need into the function that needs it):

    $my_settings = get_my_settings_from_db();
    
    function returnSetting($settings, $key='') {
         return $settings[$key];
    }
    
    returnSetting($my_settings, $key);
    

    Or perhaps better create a settings class:

    class Settings
    {
        protected $settings;
    
        public function __construct($settings)
        {
            $this->settings = $settings;
        }
    
        public function getSetting($key)
        {
            return $this->settings[$key];
        }
    }
    
    // you only create one instance per request, so you would do this in for example your bootstrap file
    $settings = new Settings($my_settings);
    

    Let’s say you have a class which needs some settings you could do the following:

    class Foo
    {
        protected $settings;
    
        public function __construct($settings)
        {
            $this->settings = $settings;
        {
    
        public function bar()
        {
            // do something which requires a setting
            echo $this->settings->getSetting('dimensions');
        }
    }
    
    $settings = new Settings($my_settings);
    $foo = new Foo($settings);
    $foo->bar();
    

    When doing it this you will make you code better testable and maintainable.

    Note this is just something I written with a beer in one hand and it is untested, but you should get the idea. 🙂

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.