Viewed   124 times

I have code something like this:

<?
    $a="localhost";
    function body(){
        global $a;
        echo $a;
    }

    function head(){
        global $a;
        echo $a;
    }

    function footer(){
        global $a;
        echo $a;
    }
?>

is there any way to define the global variable in one place and make the variable $a accessible in all the functions at once? without making use of global $a; more?

 Answers

4

The $GLOBALS array can be used instead:

$GLOBALS['a'] = 'localhost';

function body(){

    echo $GLOBALS['a'];
}

From the Manual:

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.


If you have a set of functions that need some common variables, a class with properties may be a good choice instead of a global:

class MyTest
{
    protected $a;

    public function __construct($a)
    {
        $this->a = $a;
    }

    public function head()
    {
        echo $this->a;
    }

    public function footer()
    {
        echo $this->a;
    }
}

$a = 'localhost';
$obj = new MyTest($a);
Saturday, November 12, 2022
4

Well, why don't you just test ? ;-)

Note: It is not as easy as you'd think -- read the full answer ;-)


Calling the hello_testing(); function, without setting the variable:

hello_testing();

I get no output -- which indicates isset returned false.


Calling the function, after setting the variable:

$conditional_random = 'blah';
hello_testing();

I get an output:

foo is inside

Which indicates global works as expected, when the variable is set -- well, one should not have any doubt about that ^^



But note that isset will return false if a variable is set, and null!


See the manual page of isset()

Which means that a better test would be:

function hello_testing() {
    global $conditional_random;
    var_dump($conditional_random);
}

hello_testing();

And this displays:

null

No Notice: the variable exists! Even if null.

As I didn't set the variable outside of the function, it shows that global sets the variable -- but it doesn't put a value into it; which means it's null if not already set outside the function.


While:

function hello_testing() {
    //global $conditional_random;
    var_dump($conditional_random);
}

hello_testing();

Gives:

Notice: Undefined variable: conditional_random

It proves that notices are enabled ;-)

And, if global didn't "set" the variable, the previous example would have given the same notice.


And, finally:

function hello_testing() {
    global $conditional_random;
    var_dump($conditional_random);
}

$conditional_random = 'glop';
hello_testing();

Gives:

string 'glop' (length=4)

(This is to purely to demonstrate my example is not tricked ^^)

Saturday, October 8, 2022
 
2

Ideally, everything your function needs should be in a parameter.

function Sum($a, $b) {
  return $a + $b;
}

If you really can't avoid referring to variables outside your function scope, you have several options:

Use the $GLOBALS array:

function Sum() {
  return $GLOBALS['a'] + $GLOBALS['b'];
}

Use the global keyword:

function Sum()
  global $a, $b;
  return $a + $b;
}

Use an "anonymous" function and inject the globals

$Sum = function() use ($a, $b) {
  return $a + $b;
}

$result = $Sum();

Anonymous functions are more useful if you want to work with variables in-scope of the functional declaration that aren't global.

extract() them from $GLOBALS

function Sum() {
  extract($GLOBALS);
  return $a + $b;
}

This last option will pull in all globals, and seems to be what you're looking for.

But seriously, parametize whenever possible.

Tuesday, November 29, 2022
 
5

Just define your variables in global.js outside a function scope:

// global.js
var global1 = "I'm a global!";
var global2 = "So am I!";

// other js-file
function testGlobal () {
    alert(global1);
}

To make sure that this works you have to include/link to global.js before you try to access any variables defined in that file:

<html>
    <head>
        <!-- Include global.js first -->
        <script src="/YOUR_PATH/global.js" type="text/javascript"></script>
        <!-- Now we can reference variables, objects, functions etc. 
             defined in global.js -->
        <script src="/YOUR_PATH/otherJsFile.js" type="text/javascript"></script>
    </head>
    [...]
</html>

You could, of course, link in the script tags just before the closing <body>-tag if you do not want the load of js-files to interrupt the initial page load.

Tuesday, October 4, 2022
 
1

If you have to generate global variables in production code (which should be avoided) always declare them explicitly:

window.globalVar = "This is global!";

While it is possible to define a global variable by just omitting var (assuming there is no local variable of the same name), doing so generates an implicit global, which is a bad thing to do and would generate an error in strict mode.

Thursday, August 25, 2022
 
mrpotes
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :