Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Superglobals Aptitude Questions and Answers
PHP Superglobals Aptitude Questions and Answers: This section contains aptitude questions and answers on PHP Superglobals.
By Nidhi Last updated : December 15, 2023
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
Correct answer: 4
D
Explanation:
The super-globals are predefined variables that can be accessed through any function or class with doing anything special in PHP.
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
Correct answer: 1
Yes
Explanation:
Yes, PHP stores all global variables in an array called $GLOBALS. Here the name of the global variable is used as an index of $GLOBALS. For example, we have $NUM is a global variable then we use $GLOBALS['NUM'] like this.
Learn: PHP $_Global
4) What is the correct output of the given code snippets in PHP?
<?php
$NUM1 = 75;
$NUM2 = 25;
$NUM3 = 0;
function sum()
{
$NUM1 = 10;
$NUM2 = 20;
$GLOBALS['NUM3'] = $GLOBALS['NUM1'] + $GLOBALS['NUM2'];
$NUM3 = $NUM1 + $NUM2;
echo $NUM3 . " ";
}
sum();
echo $NUM3 . " ";
?>
- 30 30
- 30 100
- 100 30
- 0 30
Correct answer: 2
30 100
Explanation:
The $GLOBALS is used to access global variables anywhere in the program, here we accessed only global variables using $GLOBALS, rest of them are local variable for the function sum(). Then echo statement prints 30 within the function definition and value of $NUM3 outside the function contain the sum of global variables then it prints 100 on the web page.
5) What is the correct output of the given code snippets in PHP?
<?php
$NUM1 = 75;
$NUM2 = 25;
$NUM3 = 0;
function sum()
{
$NUM1 = 10;
$NUM2 = 20;
$GLOBALS["NUM3"] = $GLOBALS["NUM1"] + $GLOBALS["NUM2"];
$NUM3 = $NUM1 + $NUM2;
echo $NUM3 . " ";
}
echo $NUM3;
sum();
?>
- 30 30
- 30 100
- 100 30
- 0 30
Correct answer: 4
0 30
Explanation:
Here we print global $NUM3 before calling sum() function, then it will print 0 and then local variable $NUM3 will print 30 inside the sum() function.
Note: We can use a single or double quote to access the global variable in $GLOBALS.
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
Correct answer: 4
A, B, C, and D
Explanation:
All given statements are correct about $_SERVER variable.
7) What is the correct output of the given code snippets in PHP?
<?php
$NUM1 = 75;
$NUM2 = 25;
$NUM3 = 0;
function sum()
{
$NUM1 = 10;
$NUM2 = 20;
$GLOBALS["NUM3"] = $_SERVER["NUM1"] + $GLOBALS["NUM2"];
$NUM3 = $NUM1 + $NUM2;
echo $NUM3 . " ";
}
sum();
echo $NUM3;
?>
- 30 30
- 30 100
- 30 25
- Syntax error
Correct answer: 3
30 25
Explanation:
The above code will print "30 25" because $_SERVER is not used to access the global variable, but it uses value 0, so that "30 25" printed on the web page.
8) Can we use $GLOBALS in small letters ($globals) to access global variables?
- Yes
- No
Correct answer: 2
No
Explanation:
No, we cannot use $GLOBALS in small letters ($globals) to access global variables. It will not generate any error but it will not access the global variable.
9) Can we get the path of the current script using the $_SERVER variable?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, we can get the path of the current script using $_SERVER['SCRIPT_NAME'].
10) Which of the following option is used to get the IP Address of the host server?
- $_SERVER['IP_ADDR']
- $_SERVER['SERVER_ADDR']
- $_SERVER['SERVER_IP_ADDR']
- $_SERVER['SERVER_ADDRESS']
Correct answer: 2
$_SERVER['SERVER_ADDR']
Explanation:
The $_SERVER['SERVER_ADDR'] is used to get the IP address where the website is hosted.
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']
Correct answer: 3
$_SERVER['PHP_SELF']
Explanation:
The $_SERVER['PHP_SELF'] is used to get the name of currently executing script.
12) Can we access query string using the $_SERVER global variable?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, we can access the query string using $_SERVER['QUERY_STRING'] global variable.
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
Correct answer: 1
A and B
Explanation:
The $_REQUEST is a predefined superglobal variable, which is used to get data after submitting the HTML Form.
14) The given code snippet is correct to access data using $_REQUEST in PHP?
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
value: <input type="text" name="val">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$val = $_REQUEST['val'];
}
?>
</body>
</html>
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, in the above code we are accessing the value of the input field using $_REQUEST, this is the proper way to access the values after submitting the HTML form.
15) The $_POST and $_GET are also used to get data after submitting HTML form?
- Yes
- No
Correct answer: 1
Yes
Explanation:
The $_POST and $_GET variables are also used to get data after submitting the HTML form.