Javascript: Global variables

This entry is part 10 of 27 in the series Javascript Tutorial

The scope containing all of a program is called global scope.

This is the scope you are in when entering a script.  When you define a variable here, the variable can be accessed by all functions.

Inside the global scope, you can create a nested scope by defining a function.

<!DOCTYPE html>
<html>
<head>
    <title>Global Variables</title>
    
    <script>
        var global_str = 'I am a global string';

        function updateFunction() {
            var local_str = 'I am a local string';
            document.getElementsByTagName('p')[0].innerHTML = local_str;
        }
    </script>

</head>
<body>
<p>I am a string</p>
<button onclick='updateFunction()'>Click Me</button>
<br>
<script>
    document.write(global_str);
</script>
</body>
</html>

PHP: $GLOBALS example

This entry is part 13 of 54 in the series PHP Tutorial

$GLOBALS — References all variables available in global scope

It is 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.

<?php
function test() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();
?>

The above example will output something similar to:

$foo in global scope: Example content
$foo in current scope: local variable

.

PHP: Using $GLOBALS instead of global

This entry is part 11 of 54 in the series PHP Tutorial

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

The previous example can be rewritten as:

<?php
$a = 1;
$b = 2;

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

Sum();
echo $b;
?>

The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element.

Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

PHP: Variable with global keyword

This entry is part 10 of 54 in the series PHP Tutorial

In PHP, global variables must be declared global inside a function if they are going to be used in that function.

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version.

PHP: Variable scope

This entry is part 7 of 54 in the series PHP Tutorial

The scope of a variable is the context within which it is defined.

Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?>

If you type the code into a good IDE such as phpStorm, you will receive a complain from the editor saying that $a is an undefined variable.

The above script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope.

You may notice that this is different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition.