Current Location: Home> Latest Articles> PHP Predefined Arrays Explained: In-depth Guide to $_SERVER, $_GET, and $_POST

PHP Predefined Arrays Explained: In-depth Guide to $_SERVER, $_GET, and $_POST

gitbox 2025-06-12

1. Introduction to PHP Predefined Arrays

In PHP, there are certain arrays known as predefined arrays that are automatically defined in the PHP runtime environment. These arrays provide easy access to commonly used global variables and server environment information, offering developers a convenient way to handle these aspects. They can be used anywhere without additional configuration or imports.

1.1. $_SERVER

$_SERVER is an array that contains various server and execution environment information. It offers a simple way to access these details. Below are some common keys in $_SERVER:

  • 'SERVER_NAME': The hostname of the server where the script is running
  • 'REQUEST_METHOD': The method used for the current request (GET, POST, HEAD, etc.)
  • 'REMOTE_ADDR': The client's IP address
  • 'HTTP_USER_AGENT': The browser information of the client

You can access these values using $_SERVER['key_name']. For example, using $_SERVER['HTTP_USER_AGENT'] gives the client's browser information.


$userAgent = $_SERVER['HTTP_USER_AGENT'];
echo "Client's browser information: " . $userAgent;

This code will print the client's browser information, allowing developers to make necessary adjustments based on the browser type.

1.2. $_GET and $_POST

$_GET and $_POST are predefined arrays used in PHP to fetch request parameters.

$_GET is used to retrieve values passed through URL parameters, while $_POST is used to get values sent via the HTTP POST method. Both arrays can contain multiple key-value pairs, representing the names and values of the request parameters.

1.2.1. $_GET

Here is an example of fetching values from the $_GET array:


if (isset($_GET['name'])) {
    $name = $_GET['name'];
    echo "Welcome, " . $name . "!";
}

This code checks if a parameter named "name" is passed and assigns its value to the $name variable. If present, it prints a welcome message.

1.2.2. $_POST

Similar to $_GET, $_POST can be used to retrieve data submitted through forms. Here is an example of using $_POST:


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    // Logic for username and password verification...
}

This code checks if the request method is POST and retrieves the username and password from the form. This allows easy access to form data for further processing.

2. Summary

PHP's predefined arrays are powerful and convenient tools that provide developers with the ability to easily access commonly used global variables and server environment information. This article has introduced several common predefined arrays, including $_SERVER, $_GET, and $_POST, and provided usage examples.

By using these predefined arrays, developers can handle server and request-related information more efficiently, improving development productivity and reducing redundant code.