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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:59:15+00:00 2026-06-10T02:59:15+00:00

In PHP, what is the right way forward for the following situation: I have

  • 0

In PHP, what is the right way forward for the following situation:

I have a ‘central’ vars.php which contains some parameters like MySQL hostname, user, pass, a $Test variable which indicates if the code is running in a test environment or not, and some other variables which I use throughout my site.

Then I have a functions.php which contains all the functions I could use throughout my site.

In some of these functions, I might need some the variables out of the vars.php file.

What is the right way to make the vars.php available to ‘everywhere’ in PHP?
Now I am doing it like this:

vars.php:
   <?php
   if(strstr($_SERVER['HTTP_HOST'],"localhost"))
   {
       $Test = true;
   }
   ?>

functions.php:
   <?php
   function DoSomething()
   {
      include "vars.php";
      if($Test)
      {
         $String = "Test is true!";
      }
         else
         {
            $String = "Test is false!";
         }
      return $String;
   }
   ?>

index.php:
   <?php
   include "vars.php";
   include "functions.php;
   $DoSomething = DoSomething();
   echo $DoSomething;
   ?>

While this does work, I have to include the vars.php in each function I define, this doesn’t seem like a ‘nice’ solution to me.
Isn’t there a way to define the variables in vars.php directly in the global scope, and be able to use them inside functions directly without having to worry about including them?

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-10T02:59:17+00:00Added an answer on June 10, 2026 at 2:59 am

    Use constants. Define all together and always on top of classes/files.

    Constants are uniques and we use them as config info or static/const data.

    If u are sue DEFINE remember use const as a string not a var, example:

    define(“BASE_PATH”, “C://base/”);

    $dir = BASE_PATH . “images/dropbox.txt”;

    http://php.net/manual/en/language.constants.php

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

Sidebar

Related Questions

Right now I have a PHP file that does a MYSQL query and then
Which is the right way to implement php exception (try{}catch(){}) in a foreach loop
What is the right way to save (php, html, js and mysql) code in
What's the right way of using a MySQL function while using PHP PDO? The
We have a PHP project that we would like to version control. Right now
I'm writing a PHP framework right now, and have run into somewhat of a
So I'm learning PHP with MYSQL right now. Making a user registration and mail
Right now I can run a PHP script from my Mac terminal like echoing
Right now, I have: RewriteRule ^([^/\.]+)?$ index.php?id=$1 [L] to match any username at the
There is some way to execute a php script every 40 milliseconds? I don't

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.