Global statements outside of a function

I have seen PHP: Variable scope - Manual. Is there a page that is specific to the global statement? I apologize for asking a very simple question but I don’t know where to look for an answer.

I have encountered some source code that I don’t understand. It is essentially:

<?php
    global $somevar;
    $somethingelse = dosomething( $somevar );
?>

That code is not in a function yet everything I can find shows the global statement being used in functions. I don’t find something describing the use of the global statement outside of a function. Does the global statement have a use outside of a function? If so, then is there some documentation that describes that? If not then if someone wants to describe what it does then that will be appreciated.

This code you found just makes no sense.

It’s no wonder because PHP is very easy language to learn and Internet makes it very ease to publish any rubbish by someone who is just started to learn.

  • there is no point in calling global keyword outside a function
  • there is no point in using global if you are sending a variable through function parameters.

Besides, there is very little use for the global at all. Most of time you want to send a variable through parameters. Only a few really global variables could be used inside a function through global, yet even they are better be replaced by some more advanced services.

3 Likes

Thank you, @colshrapnel, I agree with your suggestions.

Oh, I did not realize that it is a plugin and not WordPress itself but now when I look at the path it is obvious the code is for a plugin. The code is in the:

wp-content/plugins/jetpack/modules/minileven/theme/pub/minileven/footer.php

file of WordPress plugins. Hopefully WordPress does not do things as obviously wrong as that. I changed the names in my sample to make the sample as simple as possible but I think my sample is an accurate representation of the actual code.

It is possible that someone copied code from somewhere else or simplified the code or something like that. They were sloppy in doing that or a real beginner.

It is also possible that this file was being included by another file from inside of a function. In which case the global keyword would have been appropriate. You could confirm this speculation by searching for the file name in other files.

Needless to say, avoid this sort of thing.

1 Like

Yes, @ahundiak, I am probably too inexperienced to understand. The file that the global is in has HTML and other stuff in it so I assumed it was not in a function. The file is included in a function so that apparently explains things. I guess I will try to blame PHP for intermingling itself among HTML.

You say to avoid this type of thing and it definitely does make things difficult for other developers.

Without knowing all of the details, the only reason I can think of is that the file is some sort of an add-on and the author wanted to be sure he would have access to global variables, even if it was included from within a function. I would still generally suggest using $GLOBALS instead or to write the code in a different way, but without knowing everywhere the file is included it would be hard to know.

As you are just getting started with PHP, my advice for your own coding would be to avoid using global variables unless absolutely necessary (it rarely, if ever, is). Having a function change/access global variables can introduce some hard to find bugs down the line. One of the only times I would use it would be if you aren’t using objects and you have some sort of global configuration array that needs to be read from within a function or similar. That is to say, even if you have to use them, I would almost always recommend treating them as read-only.

Still not a big fan, haha.

Jeff

Yes, that would do it. The global definition is therefore inside a function, so not invalid.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.