Global Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

Global Variables

Post by BobN »

Okay, I've read several pages about variables and their scope but I can't believe I have actually read everything on the subject - surely I've missed something.

Issue - global variables available in in functions in an "include" file.

Is it true that PHP has no way of specifying that a variable is globals in scope with out messing around with GLOBAL on references to a global variable or using $GLOBALS every time your reference them.

Is it true, there is no way to define a variable, set a value, and declare it as a global variable in one or more statements of code?

If it is true, who the heck designed this language?

I've been programming computers (with about a dozen languages and semi-languages) for 37+ years and this is the first language I've come across which supports global variables but makes referencing them so verbose.

It seems ass-backwards to declare that a variable is global INSIDE a function. Why not define it as Global when you define the variable and set the value?

Heck, even JavaScript, a relatively simple language, provides a quick and easy method of defining globals - you define them outside of functions.

Doesn't PHP have something similar?

Bob
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Global Variables

Post by Christopher »

You are misunderstanding globals in PHP. Like other languages, a variable declared outside of all functions and classes is in the global namespace. The difference is that in many languages global variables are automatically visible inside functions. In PHP you must explicitly declare them to make them visible with the global keyword.

Code: Select all

$foo = 'something';     // this is a global variable

function one() {
     // $foo is NOT visible here
}

function two() {
    global $foo;
     // $foo IS visible here
}
PS - Whether you dislike this syntax is irrelevant, this is how it works in PHP.

PPS - You shouldn't use global variables in this way anyway. Instead declare as few variables in the global namespace as possible and always inject variables into functions and objects.
(#10850)
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

Re: Global Variables

Post by BobN »

So as not to misunderstand the way PHP does it, compared to other languages:

What, presicely, do you mean when you say
. . . inject variables into functions and objects.
Riyaverma
Forum Newbie
Posts: 2
Joined: Fri Feb 03, 2012 1:14 am

Re: Global Variables

Post by Riyaverma »

Thanks for sharing the this helpful post.I am facing the same problem.
I confuse how we manage the global varible through the program and how we call it in throughout the program.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Global Variables

Post by Christopher »

By injecting, I mean:

Code: Select all

$foo = 'something';     // this is a global variable

function one($foo) {
     // $foo IS visible here - GOOD
}

function two() {
    global $foo;
     // $foo IS visible here - BAD
}
With function one() you can follow the variable $foo down through calls to see where it is used. With the global some totally unrelated code can change the global $foo without you knowing where it happened. These are called side-effects and code is more maintainable without them.
(#10850)
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

Re: Global Variables

Post by BobN »

How about good old OOP - "encapsulating the data with the function" (OOP is a bit overblown in my opinion)?

Code: Select all

<?php

function getSetValue($action, $newValue = NULL){
     STATIC $statVariable;
     switch (strtolower($action)) {
          case "set";
               if ($newValue === NULL) {
                    echo "<br>You did not pass a new value on a GET call";
                    return false;
               }
               $statVariable = $newValue;
               return true;
               break;
          case "get";
               return $statVariable;
               break;
          default:
               echo "<br>You passed an invalid action '" . $action . "'";
               return false;
               break;
     };
};
function main() {
     $br = "<br>";
     getSetValue("ssss",20);
     getSetValue("set");
     getSetValue("gggg");
     getSetValue("set",20);
     echo $br . "Should be 20: " . getSetValue("get");
     getSetValue("set",45);
     echo $br . "Should be 45: " . getSetValue("get");
     getSetValue("set",101);
     echo $br . "Should be 101: " . getSetValue("get");
     getSetValue("set",-1);
     echo $br . "Should be -1: " . getSetValue("get");
     getSetValue("set",false);
     echo $br . "Should be null: '" . getSetValue("set") . "'";
};

main();
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Global Variables

Post by Christopher »

BobN wrote:How about good old OOP - "encapsulating the data with the function"
What you show is not OOP ... maybe it is "good old OOP"? It is important to note that one of the most important things about OOP is that is provides a solution to exactly the problem we are discussing. An object is a private namespace that contains variables and the functions that can operate on those variables. This helps reduce side-effects.
BobN wrote:(OOP is a bit overblown in my opinion)?
I am not sure how the methodology as ordinary as OOP can be called "overblown"? But I suppose that's what Assembly programmers say about block-structured languages!
(#10850)
Post Reply