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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:57:12+00:00 2026-06-11T15:57:12+00:00

I would like to merge two php projects. Both projects use global variables so

  • 0

I would like to merge two php projects. Both projects use global variables so I would like to have separate global scopes.

Example:

file project1.php:

$var1 = 'var1';

function print1() {
  global $var1;
  echo $var1;
}

print1();

file project2.php:

function load1() {
  include('project1.php');
}

$var2 = 'var2';

function print2() {
  global $var2;
  echo $var2;
}

print2();
load1();

This would execute project2 just as expected but project1 would fail because $var1 is not in global scope but in the scope of load1().

One possible fix would be to call global $var1 in load1() but that would mix the two global scopes (messy) and would be complex since the reallive project2 has many global variables and I would have to check if there are any new ones with each update.

So it would be best if I could create a seperate global scope for project1. Is that possible?

  • 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-11T15:57:13+00:00Added an answer on June 11, 2026 at 3:57 pm

    It is not possible to have separate GLOBAL contexts. The sole purpose of the global of the global context is to be “global” whatever happens.

    In order to fix this, you should “contextualize” your code refactoring global variables to function arguments :

    function print2() {
      global $var2;
      echo $var2;
    }
    

    Becomes :

    function print2($var2) {
      echo $var2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file and I would like to merge two different CONTACT
I have two classes that I would like to merge into a composite. These
I have two images I would like to merge then save to a new
We have two PHP5 objects and would like to merge the content of one
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I would like to merge two files by one column in unix. I have
I have a wordpress page which has two loops like so... <?php global $post;
I have two lists of objects, using Linq I would like to merge them,
That one is a tricky one I believe. I would like to merge two
I would like to be able to merge two files into one during configure

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.