W3docs

global

The "global" keyword is used in PHP to access a variable declared outside the current function or class. In this article, we will explore the syntax and usage

The PHP "global" Keyword: A Comprehensive Guide

In PHP, variables have a limited scope — a variable created inside a function is local to that function and cannot see variables defined in the main script, and vice versa. The global keyword bridges that gap: it tells a function to use the same variable that lives in the global (script-level) scope instead of creating a new local one.

This page covers the syntax of global, how variable scope works, the relationship between global and the $GLOBALS superglobal, practical examples, common gotchas, and when you should reach for a cleaner alternative.

Understanding variable scope

By default, a variable defined outside a function is not visible inside it:

<?php
$message = "Hello";

function show() {
  echo $message; // Notice: Undefined variable $message
}

show();

The function show() has no access to $message because $message lives in the global scope and show() has its own separate local scope. The global keyword is one way to give the function access to that outer variable.

Syntax

The "global" keyword is used to access a global variable in PHP. Here is the basic syntax for using the "global" keyword:

The PHP syntax of global

<?php
$variableName = "initial value";

function functionName() {
  global $variableName;
  // code to be executed
}

In this example, the global keyword is used to access a variable called $variableName that is declared outside the current function. Once declared global, any change the function makes to $variableName is reflected in the outer scope, and any change made outside is visible inside the function.

You can also import several global variables at once by separating them with commas:

<?php
$x = 5;
$y = 10;

function addThem() {
  global $x, $y;
  echo $x + $y; // Output: 15
}

addThem();

Examples

Let's look at some practical examples of how the "global" keyword can be used:

Examples of PHP global

<?php

// Example 1
$counter = 0;

function incrementCounter()
{
  global $counter;
  $counter++;
}

incrementCounter();
echo $counter . PHP_EOL; // Output: 1

// Example 2
$color = "red";

function changeColor()
{
  global $color;
  $color = "blue";
}

changeColor();
echo $color; // Output: blue

In these examples, we use the global keyword to access variables declared outside the current function and modify their values.

global vs. the $GLOBALS array

PHP also exposes every global variable through the $GLOBALS superglobal — an associative array keyed by variable name. Unlike global, $GLOBALS is available in every scope without declaration, so the following two functions are equivalent:

<?php
$total = 100;

function withGlobal() {
  global $total;
  $total += 1;
}

function withGlobals() {
  $GLOBALS['total'] += 1;
}

withGlobal();
withGlobals();
echo $total; // Output: 102

$GLOBALS is handy when you need only one or two outer variables, since it avoids a separate global declaration line. Both approaches read from and write to the same underlying variables.

Common pitfalls

  • global only works for variables that already exist or that you intend to create in the global scope. Mistyping the variable name silently creates a different global variable rather than raising an error.
  • It must come before you use the variable. Declare global $name; at the top of the function, before any reference to $name.
  • global does nothing for constants or static variables — it is exclusively for ordinary global-scope variables.
  • Hidden dependencies. A function that relies on global can behave differently depending on script state, which makes it harder to test in isolation.

Benefits

Using the "global" keyword has several benefits, including:

  • Access to global variables: The "global" keyword allows you to access and modify variables declared outside the current function or class.
  • Note on maintainability: While global provides direct access to outer scope variables, overusing it can make code harder to debug and test. Modern PHP development often prefers passing variables as function parameters, using dependency injection, or accessing the $GLOBALS superglobal array when necessary.

Conclusion

In conclusion, the "global" keyword allows PHP developers to access variables declared outside the current function or class. While it can be useful for quick scripts or legacy code, modern PHP best practices recommend limiting its use to avoid tight coupling and maintainability issues. For better architecture, consider passing variables as parameters, using dependency injection, or leveraging the $GLOBALS superglobal array.

To go deeper, read about PHP variable scope, the static keyword for variables that persist between calls, and PHP variables in general.

Practice

Practice
What is true about the variable scope in PHP, based on the content of the specified URL?
What is true about the variable scope in PHP, based on the content of the specified URL?
Was this page helpful?