You get a bonus - 1 coin for daily activity. Now you have 1 coin

Global variables in php

Lecture



Global variables are variables that are available to the entire program, including subroutines (user-defined functions).

Local variables are variables defined inside a subroutine (user function). They are available only within the function in which they are defined.

For PHP, all variables declared and used in a function are by default local to the function. That is, by default there is no possibility to change the value of the global variable in the function body.

If you use a variable in the body of a user-defined function with a name identical to the name of a global variable (located outside a user-defined function), then this local variable will have no relation to the global variable relationship. In this situation, a local variable will be created in the user function with the same name as the global variable, but this local variable will be available only inside this user function.

Let us clarify this fact with a specific example:

<?php
$a = 100;

function funct() {
$a = 70;
echo "<h4>$a</h4>";
}
funct();
echo "<h2>$a</h2>";
?>

The script displays first 70 and then 100:

70
100

To get rid of this drawback, in PHP there is a special global instruction that allows the user function to work with global variables. Consider this principle with specific examples:

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

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

$b = $a + $b;
}

Sum();
echo $b;
?>

The above script will output " 3 ". After defining $ a and $ b inside a function as a global, all references to any of these variables will point to their global version. There are no restrictions on the number of global variables that can be processed by user-defined functions.

The second way to access global scope variables is to use a special, PHP-defined array $ GLOBALS . The previous example can be rewritten as:

Using $ GLOBALS instead of global:

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

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

Sum();
echo $b;
?>

$ GLOBALS is an associative array, the key of which is the name and the value is the contents of the global variable. Note that $ GLOBALS exists in any scope, this is because this array is superglobal . Below is an example demonstrating the capabilities of superglobal variables:

<?php
function test_global()
{
// Большинство предопределенных переменных не являются
// "супер" и чтобы быть доступными в локальной области
// видимости функции требуют указания 'global'.
global $HTTP_POST_VARS;

echo $HTTP_POST_VARS['name'];

// Суперглобальные переменные доступны в любой области
// видимости и не требуют указания 'global'.
// Суперглобальные переменные доступны, начиная с PHP 4.1.0
echo $_POST['name'];
}
?>

See also:

created: 2016-01-25
updated: 2021-03-13
146



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)