PHP Manual
/
Variables

Global variables in PHP

22. 08. 2019

Global variables are available at any time in any part of the application and do not need to be passed.

Warning: A well-designed application should not use global variables because they violate the encapsulation principle and can cause hard-to-detect errors if handled carelessly.

Example usage:

$a = 1;
$b = 2;
function sum(): void
{
global $a, $b;
$b = $a + $b;
}
sum();
echo $b; // prints the number 3, because the variable $b is global

Note that we have retrieved the variable $a and $b outside their natural context. This behavior is referred to as "magical" because if another function overrides the variables currently in use, the application will experience an unexpected condition.

Properly, the application should encapsulate and pass the variables each time:

$a = 1;
$b = 2;
function sum(int $a, int $b): int
{
return $a + $b;
}
echo sum($a, $b); // prints 3

This allows us to call the function dynamically with different input parameters and its output will depend only on the inputs, not the environment.

Getting input parameters from URL

Perhaps the only sensible use of global variables is in parsing user input, in which case we're talking about superglobal variables.

In this case, it is a clean design because the variable should be read-only, not write-only, and moreover, it is the same throughout the application:

function getNameFromUrl(): string
{
return isset($_GET['name'])
? htmlspecialchars($_GET['name'])
: '';
}
echo getNameFromUrl();

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

1.
4.
Status:
All systems normal.
2024