EZ

Eduzan

Learning Hub

Eduzan
Eduzan / PHP

PHP Advance

Introduction to Superglobal Variables in PHP

Superglobals are built-in array variables in PHP that allow developers to access important data about requests or scripts. These superglobals can be accessed globally from any part of your code, such as functions or classes, without requiring special declarations like global variables. They are especially useful for handling data transfer between pages, dealing with requests, and managing server information.

Here’s a list of the available superglobal variables in PHP:

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

Let’s take a closer look at some of these superglobals:

1. $GLOBALS Superglobal :$GLOBALS is used to access global variables from any part of the PHP script. PHP stores all global variables in the $GLOBALS array, where the key is the variable name, and the value can be accessed.

Example:

<?php
$a = 10;
$b = 20;

function addition() {
    $GLOBALS['result'] = $GLOBALS['a'] + $GLOBALS['b'];
}

addition();
echo $result;
?>

Output:

30

2.$_SERVER Superglobal:$_SERVER holds information about headers, paths, and script locations. It provides valuable details about the server and the current script.

Example:

<?php
echo $_SERVER['PHP_SELF'];  // Current script
echo "<br>";
echo $_SERVER['SERVER_ADDR'];  // Server IP
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];  // Browser details
echo "<br>";
echo $_SERVER['REQUEST_METHOD'];  // HTTP method used
?>

Output:

/path/to/script.php
127.0.0.1
Mozilla/5.0 (your browser details)
GET

3.$_REQUEST Superglobal$_REQUEST is a superglobal that collects data from forms, whether submitted using GET or POST. Although it’s versatile, developers often prefer using $_POST or $_GET for specific requests.

Example:

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="username">
  <input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_REQUEST['username'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo "Hello, " . $name;
    }
}
?>
</body>
</html>

Output:

Hello, John

4.$_POST Superglobal:$_POST is used to collect data from forms when they are submitted using the POST method. The data is not visible in the URL, ensuring privacy and security.

Example:

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Enter your name: <input name="username" type="text"><br>
  Enter your age: <input name="age" type="text"><br>
  <input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['username'];
    $age = $_POST['age'];
    echo "$name is $age years old.";
}
?>
</body>
</html>

Output:

Jane is 25 years old.

5. $_GET Superglobal$_GET is used to collect data from forms submitted using the GET method. Data sent this way is visible in the URL, making it less secure but useful in certain scenarios.

Example:

<!DOCTYPE html>
<html>
<head>
<title>$_GET Example</title>
</head>
<body>

<?php
// Collecting values sent in the URL
$name = $_GET['username'];
$city = $_GET['city'];

echo "<h2>Welcome, $name from $city!</h2>";
?>

</body>
</html>

Assuming URL

http://localhost/page.php?username=Alice&city=NewYork

Output:

Welcome, Alice from NewYork!
End of lesson.