2

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?

5
  • 1
    Try to print $GLOBALS with vardump and post the output. Commented Aug 23, 2011 at 18:43
  • 1
    Post some actual code and we'll be able to help more easily. Commented Aug 23, 2011 at 18:43
  • Write clean code, instead of using global variables. Commented Aug 23, 2011 at 18:45
  • @leo what exactly I should post from $GLOBALS? There are about few hundred of variables there. Commented Aug 23, 2011 at 18:54
  • @Michael there are a lot of functions that does not have anything to do with this problem Commented Aug 23, 2011 at 18:56

1 Answer 1

3

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.

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

5 Comments

thats what I tried, but echo $GLOBALS['something'] still output nothing. I appreciate your advice about constants, but Ill stick to globals now.
@Treat: Are you sure the files are included and the globals registered before the code using them is called?
@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?
I am sure that you have checked php.net/manual/en/language.variables.scope.php
@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 echoed $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?

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.