Yes. That’s how PHP works.
With PHP If you just use “include” to include header, sidebar etc, varibales declared in an include earlier on will be available to code in includes that follow it.
I have tried already to put all code in plug-ins but the problem is the same.