Questions tagged [global]

Refers to a space where variables and other items may be accessed from any scope.

4669 questions
101
votes
16 answers

PHP pass variable to include

I'm trying to pass a variable into an include file. My host changed PHP version and now whatever solution I try doesn't work. I think I've tried every option I could find. I'm sure it's the simplest thing! The variable needs to be set and evaluated…
user1590646
  • 1,011
  • 2
  • 7
  • 4
99
votes
6 answers

How can I access global variable inside class in Python

I have this: g_c = 0 class TestClass(): global g_c def run(self): for i in range(10): g_c = 1 print(g_c) t = TestClass() t.run() print(g_c) how can I actually modify my global variable g_c?
torayeff
  • 9,296
  • 19
  • 69
  • 103
94
votes
5 answers

Static vs global

If I have a C file like below, what is the difference between i and j? #include #include static int i; int j; int main () { //Some implementation }
Vijay
  • 65,327
  • 90
  • 227
  • 319
93
votes
6 answers

Are global variables in PHP considered bad practice? If so, why?

function foo () { global $var; // rest of code } In my small PHP projects I usually go the procedural way. I generally have a variable that contains the system configuration, and when I nead to access this variable in a function, I do…
KRTac
  • 2,767
  • 4
  • 22
  • 18
91
votes
3 answers

Preserving global state in a flask application

I am trying to save a cache dictionary in my flask application. As far as I understand it, the Application Context, in particular the flask.g object should be used for this. Setup: import flask as f app = f.Flask(__name__) Now if I do: with…
Profpatsch
  • 4,918
  • 5
  • 27
  • 32
90
votes
6 answers

Non-declaration statement outside function body in Go

I'm building a Go library for an API that offers JSON or XML formatted data. This API requires me to request a session_id every 15 minutes or so, and use that in calls. For…
sergserg
  • 21,716
  • 41
  • 129
  • 182
86
votes
5 answers

Passing a variable from one php include file to another: global vs. not

I'm trying to pass a variable from one include file to another. This is NOT working unless I declare the variable as global in the second include file. However, I do NOT need to declare it as global in the file that is calling the first include. For…
maxedison
  • 17,243
  • 14
  • 67
  • 114
84
votes
10 answers

How to get the global object in JavaScript?

I want to check in a script if a certain other module is already loaded. if (ModuleName) { // extend this module } But if ModuleName doesn't exist, that throws. If I knew what the Global Object was I could use that. if (window.ModuleName) { …
coolaj86
  • 74,004
  • 20
  • 105
  • 125
74
votes
3 answers

How to access global variables

How do I access a variable that was declared/init in my main.go in a different .go package/file? Keeps telling me that the variable is undefined (I know that global variables are bad but this is just to be used as a timestamp) in main.go var…
Nighthee
  • 1,329
  • 3
  • 13
  • 14
74
votes
7 answers

Swift: Global constant naming convention?

In Swift, it seems that global constants should be camelCase. For example: let maximumNumberOfLoginAttempts = 10 Is that correct? I'm used to all caps, e.g., MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS, from C, but I want to acquiesce to Swift conventions.
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
71
votes
10 answers

Reason for globals() in Python?

What is the reason of having globals() function in Python? It only returns dictionary of global variables, which are already global, so they can be used anywhere... I'm asking only out of curiosity, trying to learn python. def F(): global x …
user1632861
69
votes
9 answers

how to create global function that can be accessed from any controller and blade file

I have two controller file homecontroller and backendcontroller. What is the best way to create global function and access it from both files? I found here Arian Acosta's answer helpful but I wonder if there is an easiest way. I would appreciate…
Johnny
  • 1,685
  • 6
  • 24
  • 42
65
votes
4 answers

JavaScript: Global variables after Ajax requests

the question is fairly simple and technical: var it_works = false; $.post("some_file.php", '', function(data) { it_works = true; }); alert(it_works); # false (yes, that 'alert' has to be here and not inside $.post itself) What I want to…
Gal
  • 23,122
  • 32
  • 97
  • 118
62
votes
12 answers

Do you use the "global" statement in Python?

I was reading a question about the Python global statement ( "Python scope" ) and I was remembering about how often I used this statement when I was a Python beginner (I used global a lot) and how, nowadays, years later, I don't use it at all, ever.…
Aurelio Martin Massoni
  • 1,706
  • 3
  • 14
  • 19
61
votes
14 answers

Where are constant variables stored in C?

I wonder where constant variables are stored. Is it in the same memory area as global variables? Or is it on the stack?
user188276