PHP provides a set of predefined variables that are always available in your scripts, regardless of scope. These variables give you access to request data, server information, session state, environment variables, and more. Many of them are known as superglobals, which means they are available everywhere in your script without needing to use global.
- Superglobals in PHP
$GLOBALS
The $GLOBALS array contains all variables that are currently defined in the global scope of the script. It can be used to access global variables inside functions or methods.
$name = "Orkhan"; function showName() { echo $GLOBALS['name']; // Access global variable inside function } showName(); // Orkhan
$_SERVER
The $_SERVER array provides information about the server environment and the request. It includes details such as headers, script locations, and request methods.
echo "You are using: " . $_SERVER['HTTP_USER_AGENT']; echo "Server Name: " . $_SERVER['SERVER_NAME']; echo "Request Method: " . $_SERVER['REQUEST_METHOD'];
$_GET
The $_GET array stores variables passed to the script via the URL query string.
- Example (URL: index.php?name=Orkhan&age=25):
echo "Name: " . $_GET['name']; echo "Age: " . $_GET['age'];
$_POST
The $_POST array contains data sent via the HTTP POST method, often used in HTML forms.
<!-- form.html --> <form method="post" action="submit.php"> <input type="text" name="username"> <input type="submit" value="Submit"> </form>
// submit.php echo "Username: " . $_POST['username'];
$_FILES
The $_FILES array is used for file uploads via HTTP POST.
<!-- upload.html --> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload"> <input type="submit" value="Upload"> </form>
// upload.php if (isset($_FILES['fileToUpload'])) { echo "File name: " . $_FILES['fileToUpload']['name']; echo "File size: " . $_FILES['fileToUpload']['size']; }
$_COOKIE
The $_COOKIE array stores data sent to the script by the client's browser via cookies.
// Set a cookie setcookie("username", "Orkhan", time() + 3600); // Access cookie if (isset($_COOKIE['username'])) { echo "Hello, " . $_COOKIE['username']; }
$_SESSION
The $_SESSION array stores information about a user's session, which persists across multiple page requests.
session_start(); $_SESSION['user'] = "Orkhan"; echo "Session set.";
// another.php session_start(); echo "Welcome back, " . $_SESSION['user'];
$_REQUEST
The $_REQUEST array contains data from GET, POST, and COOKIE. It's a more general collection, but using $_GET and $_POST directly is recommended for clarity.
- Example (URL: form.php?name=Orkhan):
echo "Hello, " . $_REQUEST['name'];
$_ENV
The $_ENV array holds environment variables passed to the script, often set at the server level.
print_r($_ENV); // Display all environment variables
$http_response_header
When using functions like file_get_contents() on a URL, PHP automatically populates $http_response_header with the HTTP response headers.
$content = file_get_contents("https://www.example.com"); print_r($http_response_header);
$argc
This variable contains the number of arguments passed to the script when run from the command line.
- Example (CLI):
php script.php arg1 arg2
echo "Number of arguments: " . $argc; // 3 (script name + 2 args)
$argv
The $argv variable is an array of arguments passed to the script via the CLI.
- Example (CLI):
php script.php Orkhan 25
print_r($argv);
Array ( [0] => script.php [1] => Orkhan [2] => 25 )
Source: Orkhan Alishov's notes