so I have site structure like this. I have index.php, that includes() include.php, which includes functions.php and a bunch of other files.
What I want to do is write $GLOBALS["something"] = 'something here'; in functions.php and after do echo $something; in index.php so it would print something here, but for some reason it returns nothing. Where is my mistake?
-
1Try to print $GLOBALS with vardump and post the output.evotopid– evotopid2011-08-23 18:43:55 +00:00Commented Aug 23, 2011 at 18:43
-
1Post some actual code and we'll be able to help more easily.Michael Berkowski– Michael Berkowski2011-08-23 18:43:59 +00:00Commented Aug 23, 2011 at 18:43
-
Write clean code, instead of using global variables.KingCrunch– KingCrunch2011-08-23 18:45:49 +00:00Commented Aug 23, 2011 at 18:45
-
@leo what exactly I should post from $GLOBALS? There are about few hundred of variables there.Treat– Treat2011-08-23 18:54:29 +00:00Commented Aug 23, 2011 at 18:54
-
@Michael there are a lot of functions that does not have anything to do with this problemTreat– Treat2011-08-23 18:56:03 +00:00Commented Aug 23, 2011 at 18:56
Add a comment
|
1 Answer
In index.php you either have to say echo $GLOBALS['something'] or global $something; echo $something; in order to register $something as a global variable.
However, I would discourage using global variables at all and instead use constants if you have to.
5 Comments
Treat
that
s what I tried, but echo $GLOBALS['something'] still output nothing. I appreciate your advice about constants, but Ill stick to globals now.NikiC
@Treat: Are you sure the files are included and the globals registered before the code using them is called?
Treat
@NikiC when you asked it like that, I understood that the globals are defined in a function and moved them outside a function and now they give output in index.php. Is there a way to define them in function and still get an output in index.php?
mozillanerd
I am sure that you have checked php.net/manual/en/language.variables.scope.php
Treat
@mozillanerd yup actually it was open in other tab, when i read your post. So I tried many ways, ex: function hey() { global $something; $something == "something here"; } and i echo
ed $something in index.php and it didnt show anything. I also tried with $GLOBALS, also nothing. Can you please tell me where the mistake is or give me a better example than in php.net?