superglobal

Superglobals in PHP

Superglobals in PHP

Superglobals are built-in variables that are always accessible, regardless of scope. Common superglobals include $_GET, $_POST, $_SESSION, and $_SERVER.

Using $_GET

$_GET is used to collect data sent via the URL query string:

      
        <?php
          // Example: index.php?name=John
          echo $_GET["name"]; // Outputs John
        ?>
      
    

Using $_POST

$_POST is used to collect data sent via the HTTP POST method:

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

        <?php
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
            echo $_POST["name"];
          }
        ?>
      
    

Activity

Try It Yourself!

Create a form that collects a user's email address using $_POST and displays it on the same page.

Quiz

Quick Quiz

  1. What is the difference between $_GET and $_POST?