In PHP, global variables are variables that can be accessed from anywhere in the PHP script, including functions and classes. PHP has several predefined global variables, known as superglobals, which can be accessed from anywhere in the script without the need to declare them as global. The superglobals are:
- $_SERVER : contains server and execution environment information, such as the current script’s filename, request method, and client IP address.
- $_GET : contains data submitted through a GET request, in the form of key-value pairs in the URL query string.
- $_POST : contains data submitted through a POST request, in the form of key-value pairs in the HTTP request body.
- $_REQUEST : contains both $_GET and $_POST data, as well as any cookies sent with the request.
- $_SESSION : contains session data that is persisted across multiple requests.
- $_COOKIE : contains data stored in cookies sent with the request.
- $_FILES : contains information about files uploaded through a form, including the file name, size, and temporary location.
- $_ENV : contains environment variables that are set on the server.
All superglobals are automatically available within the script’s scope, and their values can be accessed or modified as needed. However, it is important to be careful when using superglobals, as they are accessible from anywhere in the script and can be modified by any part of the script, which could lead to unexpected behavior.