Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available in all scopes.
PHP Global Variables - Superglobals
Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
The PHP superglobal variables are:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
The next chapters will explain some of the superglobals, and the rest will be explained in later chapters.
Since PHP 5.4, you cannot use a superglobal as the parameter to a function. This causes a fatal error:
function foo[$_GET] {
// whatever
}
It's called "shadowing" a superglobal, and I don't know why people ever did it, but I've seen it out there. The easy fix is just to rename the variable $get in the function, assuming that name is unique.
There was no deprecation warning issued in previous versions of PHP, according to my testing, neither in 5.3 nor 5.2. The error messages in 5.4 are:
Fatal error: Cannot re-assign auto-global variable _GET in...
Fatal error: Cannot re-assign auto-global variable _COOKIE in...
etc.
Home » Aptitude Questions and Answers » PHP Aptitude Questions and Answers
PHP Superglobals Aptitude Questions and
Answers: This section contains aptitude questions and answers on PHP Superglobals.
Submitted by Nidhi, on June 01, 2020
This section contains Aptitude Questions and Answers on PHP Superglobals.
1] There are the following statements that are given below, which of them are correct about super-globals in PHP?
- The super-global are predefined variables used in PHP.
- The super-global variables can be accessed through any function.
- The super-global variables can be accessed through any class.
- All of the above
Options:
- A and B
- A and C
- B and C
- D
2] Which of the following are correct superglobal variables used in PHP?
- $GLOBALS
- $_REQUEST
- $_COOKIE
- $_SESSION
Options:
- A and B
- C and D
- A, B, and C
- A, B, C, and D
3] PHP stores all global variables in an array called $GLOBALS?
- Yes
- No
4] What is the correct output of the given code snippets in PHP?
- 30 30
- 30 100
- 100 30
- 0 30
5] What is the correct output of the given code snippets in PHP?
- 30 30
- 30 100
- 100 30
- 0 30
6] There are the following statements that are given below, which of them are correct about $_SERVER in PHP?
- The $_SERVER is a superglobal variable, it stores information like server name, headers, and paths.
- The $_SERVER is used to get the IP address of the server.
- We can get the hostname of the user using $_SERVER variable.
- We can get URI of the current web page using $_SERVER variable.
Options:
- A and B
- C and D
- A, B, and C
- A, B, C, and D
7] What is the correct output of the given code snippets in PHP?
- 30 30
- 30 100
- 30 25
- Syntax error
8] Can we use $GLOBALS in small letters [$globals] to access global variables?
- Yes
- No
9] Can we get the path of the current script using the $_SERVER variable?
- Yes
- No
11] Which of the following option is used to get the name of currently executing script?
- $_SERVER['SCR_NAME']
- $_SERVER['SCRIPT']
- $_SERVER['PHP_SELF']
- $_SERVER['PHP_SCRIPT']
12] Can we access query string using the $_SERVER global variable?
- Yes
- No
13] There are the following statements that are given below, which of them are correct about $_REQUEST in PHP?
- It is a pre-defined global variable.
- It is used to get data after submitting the HTML form.
- We can get data without submitting the HTML form.
- None of the above
Options:
- A and B
- A and C
- C
- D
14] The given code snippet is correct to access data using $_REQUEST in PHP?
- Yes
- No
15] The $_POST and $_GET are also used to get data after submitting HTML form?
- Yes
- No