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?
  1. The super-global are predefined variables used in PHP.
  2. The super-global variables can be accessed through any function.
  3. The super-global variables can be accessed through any class.
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

2) Which of the following are correct superglobal variables used in PHP?
  1. $GLOBALS
  2. $_REQUEST
  3. $_COOKIE
  4. $_SESSION

Options:

  1. A and B
  2. C and D
  3. A, B, and C
  4. A, B, C, and D

3) PHP stores all global variables in an array called $GLOBALS?
  1. Yes
  2. No

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 . " ";
?>
  1. 30 30
  2. 30 100
  3. 100 30
  4. 0 30

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();
?>
  1. 30 30
  2. 30 100
  3. 100 30
  4. 0 30

6) There are the following statements that are given below, which of them are correct about $_SERVER in PHP?
  1. The $_SERVER is a superglobal variable, it stores information like server name, headers, and paths.
  2. The $_SERVER is used to get the IP address of the server.
  3. We can get the hostname of the user using $_SERVER variable.
  4. We can get URI of the current web page using $_SERVER variable.

Options:

  1. A and B
  2. C and D
  3. A, B, and C
  4. A, B, C, and D

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;
?>
  1. 30 30
  2. 30 100
  3. 30 25
  4. Syntax error

8) Can we use $GLOBALS in small letters ($globals) to access global variables?
  1. Yes
  2. No

9) Can we get the path of the current script using the $_SERVER variable?
  1. Yes
  2. No

10) Which of the following option is used to get the IP Address of the host server?
  1. $_SERVER['IP_ADDR']
  2. $_SERVER['SERVER_ADDR']
  3. $_SERVER['SERVER_IP_ADDR']
  4. $_SERVER['SERVER_ADDRESS']

11) Which of the following option is used to get the name of currently executing script?
  1. $_SERVER['SCR_NAME']
  2. $_SERVER['SCRIPT']
  3. $_SERVER['PHP_SELF']
  4. $_SERVER['PHP_SCRIPT']

12) Can we access query string using the $_SERVER global variable?
  1. Yes
  2. No

13) There are the following statements that are given below, which of them are correct about $_REQUEST in PHP?
  1. It is a pre-defined global variable.
  2. It is used to get data after submitting the HTML form.
  3. We can get data without submitting the HTML form.
  4. None of the above

Options:

  1. A and B
  2. A and C
  3. C
  4. D

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>
  1. Yes
  2. No

15) The $_POST and $_GET are also used to get data after submitting HTML form?
  1. Yes
  2. No

Learn PHP programming with our PHP tutorial.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.