1

I'm really inexperienced when it comes to PHP and hoping someone can clarify something for me when it comes to how variables are handled in PHP.

I have a PHP Web App that I created and needed to make a quick duplicate of, so I simply copy and pasted this app into a new folder on the same server.

I am wondering are there any concerns if the apps, in 2 different folders, have the exact same variable names?

I'm thinking of an accidental overwrite situation. If "no", then can someone explain to me why there is no concern?

4
  • do these variables meet at any point, like say ones includes or requires, the other? Commented Jan 26, 2012 at 3:29
  • I do not pass variables between the 2 folders. Essentially what has happened is that the user inputs the same data but the front-end design is different for each app. The system it connects to on the backend is also different. For all purposes they are separate apps just with the same code. I am planning a re-write to make this more streamlined and easier to maintain. Moving forward I'd like to do something like: Value 1 -> App1 -> Unified Engine -> Backend system 1 Value 1 -> App2 -> Unified Engine -> Backend System 2 The unified Engine being able to track and separate requests Commented Jan 26, 2012 at 3:33
  • In no way it will collide, conflict or crash in your system unless the your instructions be that.. Commented Jan 26, 2012 at 3:36
  • Thanks Fernando! That is re-assuring! Would you happen to be able to maybe explain a bit more around how PHP or the web server is able to isolate these? I assume it may have something to do with each page request getting its own "thread/process"? Commented Jan 26, 2012 at 3:41

2 Answers 2

3

No, there will not be any collisions between application global variables as long as the two application directories are truly separate and don't include files from one another. When a PHP script runs, the HTTP request that initiated it can be thought of as an isolated incident. It is separated and isolated from all other requests to the same application (even concurrently) and from other applications.

Each script gets its own variable namespace when execution starts, and that environment is terminated and deleted from memory when the script completes.

Now, if you happen to be using $_SESSION and both applications use the same value for session_name() and run on the same domain name, there is the possibility that values persisting in $_SESSION can collide between your application instances. This is simply solved by changing the value for one of the applications from the default PHPSESSID:

// Application 1
session_name("APP1");
session_start();

// Application 2
session_name("APP2");
session_start();

You probably are already aware of this, but I'll say it to be complete. Wherever possible, it is advised to abstract out aspects of the code that can be shared between the two application instances and included by both of them. This is in keeping with the DRY principle, and will save you lots of headaches if you ever have to make modifications to the code both applications share.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Michael! That is exactly what I wanted to confirm or not confirm! I am planning a re-write just didn't/hadn't had time just yet!
0

In my opnion you may or may not use indenticle variables names in different folders. this depends on the functionality of your app. you need to do a little research on google 'scop of global and local variables in php'.

In short, yes, you can duplicate the app into new folder, you just need to update/reset the paths (for example the include files paths) and database connection strnigs (if your app use database to store data).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.