Exploring PHP Superglobal Variables: A Comprehensive Guide

KolaKachi
This entry is part 5 of 21 in the series PHP Course For Absolute Beginners

In the world of PHP programming, variables are essential for storing and manipulating data. In a previous post, we delved into the basics of variables and data types. In this installment, we’ll take a closer look at a special kind of variable known as “superglobals.” These predefined variables, also called built-in variables, are unique in that they are accessible from anywhere within your PHP code, regardless of scope. Let’s dive into the world of PHP superglobal variables and explore how they work.

Understanding Superglobals:

A superglobal is a built-in variable in PHP that is readily available for use without any need for explicit declaration. These variables provide valuable information about various aspects of the environment, user interactions, and server settings. To access a superglobal, you use the “$_” (underscore followed by a capitalized word) notation, followed by the specific superglobal name and square brackets if necessary.

Exploring Different Superglobal Variables:

Here are some of the most commonly used superglobals in PHP and what they offer:

$GLOBALS

The $GLOBALS superglobal variable is an associative array containing all global variables. It allows you to access these variables from within a function’s local scope. For instance:

<?php

  $comment = 'Hi, I am a global variable';

  function displayComment() {
      echo 'Comment: ' . $GLOBALS['comment'];
  }

  displayComment(); // Outputs: Comment: Hi, I am a global variable

$_SERVER

The $_SERVER superglobal holds information about the web server and execution environment. It includes details like server name, port, script location, user agent, and more. This information is provided by the web server through HTTP headers. Access server-related data like:

<?php

  // Outputs: example.com
  echo $_SERVER['SERVER_NAME']; 
  /* 
     Outputs information about the user's browser, 
      such as its name and version
     e.g Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
  */
  echo $_SERVER['HTTP_USER_AGENT']; 

$_GET

The $_GET superglobal is used to access data sent through the URL using the GET method. It’s often used to gather user input or parameters. Example:

<?php
  //example.com?keyword=kolakachi.com
  $search_keyword = $_GET['keyword'];
  echo $search_keyword; // Outputs: kolakachi.com

$_POST

The $_POST superglobal holds data submitted to the server through a form with the POST method. Unlike $_GET, POST data isn’t visible in the URL. When you deal with form submissions using the POST method, $_POST comes into play. It stores form data in an array, accessible through field names. Remember to sanitize user input to prevent security risks. Example:

<?php
  
  $email = $_POST['email'];
  echo "Received email: $email"; // Outputs: email found in the request body

$_FILES

When users upload files through a form, the $_FILES superglobal contains information about the uploaded files. It’s a multidimensional array with attributes like name, type, size, and more. Uploading and processing files can be done as follows:

<?php

  $uploaded_file = $_FILES['document']; // the uploaded file
  $fileName = $uploaded_file['name']; // the uploaded file name
  $fileType = $uploaded_file['type']; // the uploaded file type
  $fileSize = $uploaded_file['size']; // the uploaded file size
  $tmpFilePath = $uploaded_file['tmp_name']; // the uploaded file temporary path/location

$_COOKIE

The $_COOKIE superglobal provides access to cookies set by the setcookie function. It’s an associative array containing data sent as HTTP cookies. Cookies can store small pieces of information between requests. For instance, accessing a previously set cookie:

<?php
  
  $user_cookie = $_COOKIE['username'];
  echo "Welcome back, $user_cookie!";

$_SESSION

$_SESSION enables you to share information across different pages of a site or app. This associative array stores session variables, maintaining state throughout a user’s interaction:

<?php
  
  $_SESSION['user_id'] = 123;
  echo "User ID: " . $_SESSION['user_id']; // Outputs: 123

$_REQUEST

Combining elements of $_GET, $_POST, and $_COOKIE, $_REQUEST is an associative array capturing HTTP request variables. Using the $_REQUEST variable requires careful consideration. It’s actually better to use $_GET, $_POST, and $_COOKIE instead of relying too much on the $_REQUEST variable.

$_ENV

The $_ENV superglobal holds variables passed to the script by the environment. It’s useful for accessing environment-specific settings like database credentials. Use putenv() or server configurations to set these variables and getenv() to retrieve them.

Conclusion

With this article, we’ve thoroughly covered each PHP superglobal variable and provided concise yet meaningful examples to aid your understanding. This resource serves as a valuable quick reference or cheat sheet as you navigate your daily PHP development tasks. Embrace these superglobals to streamline your coding and make the most of PHP’s versatile capabilities. Happy coding!

Series Navigation<< A Comprehensive Guide to PHP Data TypesUnderstanding PHP Operators: A Comprehensive Guide >>

Leave a Reply

Your email address will not be published. Required fields are marked *