WikiGalaxy

Personalize

PHP Superglobals

Understanding PHP Superglobals:

PHP superglobals are built-in variables that are always accessible, regardless of scope. They provide essential functionalities for handling input, output, and environment information.

$_GET

Using $_GET for Data Retrieval:

The $_GET superglobal is used to collect form data sent with the HTTP GET method. It retrieves query string parameters from the URL.


      // URL: example.com?name=John
      <?php
      $name = $_GET['name'];
      echo "Hello, " . $name;
      ?>
    

Console Output:

Hello, John

$_POST

Handling Form Data with $_POST:

The $_POST superglobal is used to collect form data sent with the HTTP POST method. It is commonly used for secure data submission.


      <form method="post">
        Name: <input type="text" name="name">
        <input type="submit">
      </form>

      <?php
      $name = $_POST['name'];
      echo "Hello, " . $name;
      ?>
    

Console Output:

Hello, [Name]

$_REQUEST

Accessing Data with $_REQUEST:

The $_REQUEST superglobal is a combination of $_GET, $_POST, and $_COOKIE. It is used to collect data without specifying the method.


      <form method="post">
        Age: <input type="text" name="age">
        <input type="submit">
      </form>

      <?php
      $age = $_REQUEST['age'];
      echo "Age: " . $age;
      ?>
    

Console Output:

Age: [Age]

$_SESSION

Managing Sessions with $_SESSION:

The $_SESSION superglobal is used to store session variables, which can be accessed across different pages on a website.


      <?php
      session_start();
      $_SESSION['username'] = "JaneDoe";
      echo "Session username is " . $_SESSION['username'];
      ?>
    

Console Output:

Session username is JaneDoe

$_COOKIE

Working with Cookies using $_COOKIE:

The $_COOKIE superglobal is used to retrieve cookie values stored in the user's browser. Cookies are used to remember information about the user.


      <?php
      setcookie("user", "John Doe", time() + (86400 * 30), "/");
      echo "User is " . $_COOKIE['user'];
      ?>
    

Console Output:

User is John Doe

$_SERVER

Accessing Server Information with $_SERVER:

The $_SERVER superglobal contains information about headers, paths, and script locations. It is useful for retrieving server and execution environment details.


      <?php
      echo "PHP Self: " . $_SERVER['PHP_SELF'];
      echo "Server Name: " . $_SERVER['SERVER_NAME'];
      ?>
    

Console Output:

PHP Self: /index.php
Server Name: localhost

$_FILES

Handling File Uploads with $_FILES:

The $_FILES superglobal is used to handle file uploads in PHP. It contains information about uploaded files, such as name, type, size, and temporary location.


      <form action="upload.php" method="post" enctype="multipart/form-data">
        Select file: <input type="file" name="fileToUpload">
        <input type="submit" value="Upload" name="submit">
      </form>

      <?php
      if ($_FILES['fileToUpload']['error'] == 0) {
          echo "File uploaded: " . $_FILES['fileToUpload']['name'];
      }
      ?>
    

Console Output:

File uploaded: example.jpg

$_ENV

Utilizing Environment Variables with $_ENV:

The $_ENV superglobal is used to retrieve environment variables. These variables provide information about the environment in which the PHP script is running.


      <?php
      echo "Path: " . $_ENV['PATH'];
      ?>
    

Console Output:

Path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025