w1coding.comAll tutorials

PHP Superglobal

PHP lessons
  1. Environment Setup
  2. Introduction
  3. Variables
  4. Basics
  5. Operators
  6. Control
  7. Loops
  8. Variables
  9. Functions
  10. Arrays
  11. Superglobal
  12. File
  13. Loops
  14. Date
  15. Loops
  16. Strings
  17. Error
  18. Class
  19. File
  20. Include
  21. Sessions
  22. Mysql
  23. Cookies
  24. Forms
  25. Cookies
  26. Regular
  27. Authenthication
  28. Fileupload
  29. Mvc
  30. Responsive

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?