Super globals in PHP are a special type of variables that are predefined and accessible from anywhere in a PHP script. They are called "super" because they have a global scope and can be accessed from any part of the script, including functions, classes, and files. Super globals are different from regular variables in several ways, including their scope, accessibility, and predefined values.
One key difference between super globals and regular variables is their scope. Regular variables have a limited scope and are only accessible within the block of code where they are defined. On the other hand, super globals have a global scope, meaning they can be accessed from anywhere in the script. This global accessibility makes super globals extremely useful in situations where data needs to be shared across different parts of a PHP application.
Another difference between super globals and regular variables is their predefined values. Super globals are automatically populated by PHP with data from various sources, such as user input, server environment, and HTTP headers. These predefined values provide developers with easy access to important information without the need for additional code. For example, the $_SERVER super global contains information about the server and the current request, including the request method, URL, and user agent.
Super globals also differ from regular variables in their naming convention. Super globals are denoted by a leading underscore followed by uppercase letters, such as $_GET, $_POST, and $_SESSION. This naming convention helps to distinguish super globals from regular variables and prevents naming conflicts.
Here are some commonly used super globals in PHP:
1. $_GET: This super global is an associative array that contains the values of variables passed to the current script via the URL parameters. For example, if the URL is "example.com?name=John&age=25", the $_GET array will contain the values ["name" => "John", "age" => "25"].
2. $_POST: This super global is also an associative array, but it contains the values of variables passed to the current script through an HTTP POST request. This is commonly used for submitting form data. For example, if a form has an input field with the name "username", the value entered by the user can be accessed as $_POST["username"].
3. $_SESSION: This super global is used for storing and accessing session data. Sessions allow you to store user-specific information across multiple page requests. The $_SESSION array is populated with data stored in the session and can be accessed throughout the user's session.
4. $_COOKIE: This super global contains the values of cookies sent by the client to the server. Cookies are small pieces of data stored on the client's machine and can be used to track user behavior or store user preferences. The $_COOKIE array allows developers to access and manipulate cookie data.
5. $_SERVER: This super global provides information about the server and the current request. It includes details such as the request method, URL, user agent, and server environment variables. For example, $_SERVER["REQUEST_METHOD"] will give the HTTP request method used to access the current script.
Super globals in PHP are special variables that have a global scope and can be accessed from anywhere in a script. They are different from regular variables in terms of scope, accessibility, and predefined values. Super globals are automatically populated by PHP with data from various sources, making them a powerful tool for accessing and manipulating important information in web applications.
Other recent questions and answers regarding Examination review:
- How can the `$_SERVER['PHP_SELF']` super global be useful when creating forms?
- What other information can be accessed from the `$_SERVER` super global? Give an example of how to access this information.
- What does `$_SERVER['REQUEST_METHOD']` return and when is it commonly used?
- How can we access the value of `$_SERVER['SERVER_NAME']` and display it on a web page?

