> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP Global Variable

A global variable in PHP is a variable declared in the main script, outside of any function, method, or class context. It resides in the global symbol table and persists throughout the execution lifecycle of the script. Unlike languages such as C or JavaScript, PHP does not automatically resolve global variables inside local scopes. To access a global variable within a function or method, explicit importation or referencing is required.

PHP provides two primary mechanisms for accessing global variables from within a local scope: the `global` keyword and the `$GLOBALS` superglobal array.

## The `global` Keyword

The `global` keyword is used inside a local scope to import a variable from the global symbol table into the local symbol table.

```php theme={"dark"}
$value = 100; // Declared in the global scope

function incrementValue() {
    global $value; // Imports the global variable into the local scope
    $value++;
}

incrementValue();
echo $value; // Outputs: 101
```

Under the hood, using the `global` keyword creates a reference (alias) to the global variable. The statement `global $value;` is semantically equivalent to:

```php theme={"dark"}
$value = &$GLOBALS['value'];
```

Because it creates a local reference, using the `unset()` function on a variable imported via the `global` keyword only severs the local reference; it does not destroy the variable in the global symbol table.

```php theme={"dark"}
$config = 'active';

function removeConfig() {
    global $config;
    unset($config); // Only destroys the local reference
}

removeConfig();
echo $config; // Outputs: active
```

## The `$GLOBALS` Superglobal Array

`$GLOBALS` is a built-in associative array containing references to all variables currently defined in the global scope. The keys of the array are the names of the global variables, and the values are the contents of those variables.

Because `$GLOBALS` is a *superglobal*, it is automatically available in all scopes without requiring explicit importation.

```php theme={"dark"}
$counter = 5;

function updateCounter() {
    // Direct access and modification via the superglobal array
    $GLOBALS['counter'] *= 2; 
}

updateCounter();
echo $counter; // Outputs: 10
```

Unlike the `global` keyword, `$GLOBALS` accesses the actual global variable directly rather than creating a local alias. Consequently, using `unset()` on an element within the `$GLOBALS` array will completely destroy the variable in the global symbol table.

```php theme={"dark"}
$session_id = 'xyz123';

function destroySession() {
    unset($GLOBALS['session_id']); // Destroys the actual global variable
}

destroySession();
var_dump(isset($session_id)); // Outputs: bool(false)
```

## Variable Variables and Global Scope

When using variable variables (dynamic variable names) with the `global` keyword, the variable holding the dynamic name is resolved in the **local** scope to determine which global variable to import.

```php theme={"dark"}
$target = 'database';
$database = 'MySQL';

function resolveDynamicGlobal() {
    global $target; // Imports $target into the local scope first
    
    // $target is resolved locally to 'database'
    // This statement is evaluated as: global $database;
    global $$target; 
    
    echo $database; // Outputs: MySQL
}

resolveDynamicGlobal();
```

PHP imposes a strict limitation regarding superglobals and variable variables: **superglobals cannot be used as variable variables inside functions or methods**. When a variable variable is evaluated dynamically, PHP does not apply the special scope-bypassing rules of superglobals. Instead, it looks for a standard local variable matching that name.

```php theme={"dark"}
$database = 'PostgreSQL';

function testSuperglobalVariable() {
    $varName = 'GLOBALS';
    
    // This fails to return the superglobal array.
    // PHP looks for a standard local variable literally named $GLOBALS.
    var_dump($$varName); // Outputs: Warning: Undefined variable $GLOBALS
}

testSuperglobalVariable();
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
