You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In PHP, superglobals are built-in global arrays that are available across all scopes (global, function, class) without the need for global declarations. They are automatically populated by the server with information, typically regarding server, environment, and user input data. These arrays are accessible from anywhere in the script.
3
+
4
+
<br/>
5
+
6
+
Here are the key PHP superglobals:
7
+
8
+
<br/>
9
+
10
+
1.`$_GET`: Contains variables passed to the script via the URL query string (i.e., after the ? in the URL).
11
+
```php
12
+
echo $_GET['name']; // URL: script.php?name=John
13
+
```
14
+
15
+
16
+
2. `$_POST`: Contains data passed to the script via an HTTP POST request (commonly from HTML forms).
17
+
```php
18
+
echo $_POST['username']; // Retrieved from an HTML form submission
19
+
```
20
+
21
+
22
+
3. `$_REQUEST`: A combination of $_GET, $_POST, and $_COOKIE data.
23
+
```php
24
+
echo $_REQUEST['email']; // Can retrieve from GET, POST, or cookies
25
+
```
26
+
27
+
28
+
4. `$_SERVER`: Contains information about headers, paths, and script locations.
29
+
```php
30
+
echo $_SERVER['HTTP_USER_AGENT']; // Shows browser information
31
+
echo $_SERVER['REQUEST_METHOD']; // Shows the request method (GET, POST, etc.)
32
+
```
33
+
34
+
35
+
5.`$_FILES`: Contains information about files uploaded via an HTTP POST request.
36
+
```php
37
+
echo $_FILES['file']['name']; // Name of the uploaded file
38
+
```
39
+
40
+
41
+
6. `$_ENV`: Contains environment variables passed to the current script.
42
+
```php
43
+
echo $_ENV['HOME']; // Access an environment variable
44
+
```
45
+
46
+
47
+
7. `$_COOKIE`: Contains variables passed to the script via HTTP cookies.
48
+
```php
49
+
echo $_COOKIE['user']; // Retrieved from the user's browser cookies
50
+
```
51
+
52
+
53
+
8. `$_SESSION`: Used to store session variables for use across multiple pages.
54
+
```php
55
+
$_SESSION['user'] = 'John';
56
+
```
57
+
58
+
9. `$GLOBALS`: It is a superglobal associative array. This superglobal can be used to access global variables from anywhere in the script. Takes variable names as the key.
59
+
```php
60
+
$x = 10;
61
+
function test() {
62
+
echo $GLOBALS['x']; // Accesses the global variable $x
0 commit comments