1

I understand that if you declare a variable within a php function with the 'global' keyword it will reference a variable declared outside the function, but why would a php programmer want to declare a variable outside of a function scope as 'global?' Thanks!

I understand what this does:

<?
$a = 1;
function $boo() {
    global $a;
    echo $a;
}
?>

But what I'm getting at is why would I want to do this?

<?
global $a;
function $boo() {
    //foo
}
?>

3 Answers 3

2

It has to do with php scope If you have file a.php that has a class like this

<?
class test()
{
  function test()
  {
    include('b.php');
  }
}
?> 

and a file b.php

<?
$a = 1;
?>

Then $a will only be accessible in the scope of function test()

if you have global $a in b.php, $a then becomes a global variable

here's the php doc about it : http://php.net/manual/en/function.include.php

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

4 Comments

Good point. Although that is basically the same situation as the asker originally presented. All includes/requires are executed within the scope the include statement is in, so you're still within a function, and using it to break out of the function scope. The include just makes it less transparent what's going on.
Yes but that is the only reason why you would see a global keyword in the main scope of a php file (ie it's included elsewhere and hence need the global to break out of the scope).
^This may be what's happening somewhere in the rest of this code. I do agree that this is can be unclear and confusing.
Good explanation. So don't do globals people, it's up there with goto's ;-)
0

I have no idea why you would want to do that. For all intents and purposes (unless I am very, very much mistaken) this is exactly the same as just:

<?
var $a;
function $boo() {
  // foo
}
?>

Which in turn is the very same as

<?
function $boo() {
  // foo
}
?>

because you generally don't have to instantiate your variables in PHP.

Very curious why you're using variably-named functions though? (function $boo() {} )

2 Comments

Yeah I'm analyzing someone else's code and I'm trying to understand what's going on. I found this and I couldn't understand why it was being done.
Wow... in that case... good luck! If they do this kind of thing, you're going to need it. (legacy codebases.. the joy, the suffering!)
0

Well, IMHO the use of global variables is a poor programming practice. It can cause unintended side-effects in your program which are hard to debug, and makes it harder to maintain.

2 Comments

While a very valid observation, this does not answer the question.
You are right. The PHP parser should throw a syntax error in this case, but it doesn't (surprise, surprise).

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.