Global variables are variables that can be accessed from anywhere in a script. There are two common ways to use them:

1. Using the global Keyword:

Variables declared outside a function are not directly accessible inside a function. Use the global keyword to access them.

<?php
$name = "John";

function displayName() {
    global $name;
    echo $name;
}

displayName();
?>

2. Using the $GLOBALS Array:

PHP stores all global variables in the associative array $GLOBALS.

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

function add() {
    echo $GLOBALS['x'] + $GLOBALS['y'];
}

add();
?>

PHP Superglobal Variables:

PHP provides built-in global arrays called superglobals, which are available in all scopes without using

SuperglobalDescription
$_GETRetrieves data sent via URL parameters.
$_POSTRetrieves data sent via HTML forms (POST method).
$_REQUESTContains data from GET, POST, and COOKIE.
$_SESSIONStores session variables.
$_COOKIEStores cookie values.
$_SERVERContains server and execution environment information.
$_FILESContains uploaded file information.
$_ENVContains environment variables.
$GLOBALSContains all global variables.