PHP PHP Tutorial PHP Forms PHP Advanced PHP OOP PHP MySQL Database PHP XML PHP - AJAX



PHP Superglobals

PHP Superglobals are predefined variables that are available in all scopes throughout a PHP script. These variables are called "superglobals" because they can be accessed from any part of the script without the need for any special syntax or function calls.

There are nine superglobal variables in PHP:

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV

$GLOBALS

The $GLOBALS superglobal is an associative array that contains references to all variables that are currently defined in the global scope of the script. This means that any variable that is defined outside of a function or class is automatically added to the $GLOBALS array.

Here is an example:

<?php
  $name = "John";
  $age = 30;

  function print_globals() {
    echo "Name: " . $GLOBALS["name"] . "<br>";
    echo "Age: " . $GLOBALS["age"] . "<br>";
  }

  print_globals();
?>

This will output:

Name: John
Age: 30

$_SERVER

The $_SERVER superglobal contains information about the server and the execution environment of the current script. This includes information such as the current script's filename, the server's IP address, and the user agent of the current request.

Here is an example:

<?php
  echo "Server name: " . $_SERVER["SERVER_NAME"] . "<br>";
  echo "Script filename: " . $_SERVER["SCRIPT_FILENAME"] . "<br>";
  echo "User agent: " . $_SERVER["HTTP_USER_AGENT"] . "<br>";
?>

This will output something like:

Server name: example.com
Script filename: /var/www/html/index.php
User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

$_GET and $_POST

The $_GET and $_POST superglobals are used to retrieve data that has been submitted to the server via a form or through a URL query string.

The $_GET superglobal is an associative array that contains the values of all the variables that were sent to the server via the URL query string. The $_POST superglobal is an associative array that contains the values of all the variables that were sent to the server via a form submission.

Here is an example:

<form method="post" action="process.php">
  <label>Name:</label>
  <input type="text" name="name"><br>
  <label>Email:</label>
  <input type="email" name="email"><br>
  <input type="submit" value="Submit">
</form>

When the form is submitted, the data can be accessed in the process.php script like this:

<?php
  $name = $_POST["name"];
  $email = $_POST["email"];

  echo "Name: " . $name . "<br>";
  echo "Email: " . $email . "<br>";
?>

$_FILES

The $_FILES superglobal is used to retrieve information about files that have been uploaded to the server via a form submission.

Here is an example:

<form method="post" action="process.php" enctype="multipart/form-data">
  <label>Upload file:</label>
  <input type="file" name="file"><br>
  <input type="submit" value="Submit">
</form>

When the form is submitted, the file information can be accessed in the process.php script like this:

<?php
  $file_name = $_FILES["file"]["name"];
  $file_type = $_FILES["file"]["type"];
  $file_size = $_FILES["file"]["size"];

  echo "File name: " . $file_name . "<br>";
  echo "File type: " . $file_type . "<br>";
  echo "File size: " . $file_size . "<br>";
?>

$_COOKIE

The $_COOKIE superglobal is used to retrieve data that has been stored in a cookie on the client's computer.

Here is an example:

<?php
  $cookie_value = $_COOKIE["username"];

  echo "Welcome back, " . $cookie_value . "!";
?>

$_SESSION

The $_SESSION superglobal is used to store data that is specific to a particular user session. This data is stored on the server and can be accessed across multiple pages during the same session.

Here is an example:

<?php
  session_start();

  $_SESSION["username"] = "John";

  echo "Welcome, " . $_SESSION["username"] . "!";
?>

$_REQUEST

The $_REQUEST superglobal is used to retrieve data that has been submitted to the server via either a GET or POST request.

Here is an example:

<form method="post" action="process.php">
  <label>Name:</label>
  <input type="text" name="name"><br>
  <input type="submit" value="Submit">
</form>

When the form is submitted, the data can be accessed in the process.php script like this:

<?php
  $name = $_REQUEST["name"];

  echo "Name: " . $name . "<br>";
?>

$_ENV

The $_ENV superglobal is used to retrieve data that has been set in the environment variables of the server.

Here is an example:

<?php
  $db_host = $_ENV["DB_HOST"];
  $db_user = $_ENV["DB_USER"];
  $db_pass = $_ENV["DB_PASS"];

  // connect to database using $db_host, $db_user, and $db_pass
?>

These are the nine superglobal variables in PHP. They are incredibly useful for accessing data from different parts of a script without having to pass variables around manually.

Reference:

Activity