This section contains Aptitude Questions and Answers on PHP Superglobals.
本节包含有关PHP Superglobals的 Aptitude问答。
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:
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.
超全局变量是PHP中使用的预定义变量。
可以通过任何函数访问超全局变量。
可以通过任何类访问超全局变量。
上述所有的
选项:
正确答案:4
d
说明:
超全局变量是预定义的变量,可以通过执行PHP中的任何特殊操作而通过任何函数或类访问。
$GLOBALS
$_REQUEST
$_COOKIE
$_SESSION
Options:
Correct answer: 4
A, B, C, and D
Explanation:
There are following superglobal variables used in PHP:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
$全球
$ _REQUEST
$ _COOKIE
$ _SESSION
选项:
正确答案:4
A,B,C和D
说明:
PHP中使用了以下超全局变量:
$全球
$ _SERVER
$ _REQUEST
$ _POST
$ _GET
$ _FILES
$ _ENV
$ _COOKIE
$ _SESSION
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.
正确答案:1
是
说明:
是的,PHP将所有全局变量存储在名为$ GLOBALS的数组中。 此处,全局变量的名称用作$ GLOBALS的索引。 例如,我们有$ NUM是一个全局变量,然后像这样使用$ GLOBALS ['NUM'] 。
<?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 . " ";
?>
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.
正确答案:2
30 100
说明:
$ GLOBALS用于访问程序中任何位置的全局变量,这里我们仅使用$ GLOBALS访问全局变量,其余的是函数sum()的局部变量。 然后,echo语句在函数定义内打印30,并且函数外部的$ NUM3值包含全局变量的总和,然后在网页上打印100。
<?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();
?>
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.
正确答案:4
0 30
说明:
这里我们在调用sum()函数之前先打印全局$ NUM3 ,然后它将打印0,然后局部变量$ NUM3将在sum()函数内部打印30。
注意:我们可以使用单引号或双引号来访问$ GLOBALS中的全局变量。
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:
Correct answer: 4
A, B, C, and D
Explanation:
All given statements are correct about $_SERVER variable.
$ _SERVER是一个超全局变量,它存储诸如服务器名称,标头和路径之类的信息。
$ _SERVER用于获取服务器的IP地址。
我们可以使用$ _SERVER变量获取用户的主机名。
我们可以使用$ _SERVER变量获取当前网页的URI。
选项:
正确答案:4
A,B,C和D
说明:
所有给定的语句关于$ _SERVER变量都是正确的。
<?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;
?>
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.
正确答案:3
30 25
说明:
上面的代码将打印“ 30 25”,因为$ _SERVER不用于访问全局变量,但它使用值0,因此在网页上打印了“ 30 25”。
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.
正确答案:2
没有
说明:
不,我们不能使用小写字母( $ globals )使用$ GLOBALS访问全局变量。 它不会产生任何错误,但不会访问全局变量。
Correct answer: 1
Yes
Explanation:
Yes, we can get the path of the current script using $_SERVER['SCRIPT_NAME'].
Correct answer: 2
$_SERVER['SERVER_ADDR']
Explanation:
The $_SERVER['SERVER_ADDR'] is used to get the IP address where the website is hosted.
正确答案:2
$ _SERVER ['SERVER_ADDR']
说明:
$ _SERVER ['SERVER_ADDR']用于获取网站托管的IP地址。
Correct answer: 3
$_SERVER['PHP_SELF']
Explanation:
The $_SERVER['PHP_SELF'] is used to get the name of currently executing script.
正确答案:3
$ _SERVER ['PHP_SELF']
说明:
$ _SERVER ['PHP_SELF']用于获取当前正在执行的脚本的名称。
Correct answer: 1
Yes
Explanation:
Yes, we can access the query string using $_SERVER['QUERY_STRING'] global variable.
正确答案:1
是
说明:
是的,我们可以使用$ _SERVER ['QUERY_STRING']全局变量访问查询字符串。
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:
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.
它是一个预定义的全局变量。
提交HTML表单后,它用于获取数据。
我们无需提交HTML表单即可获取数据。
以上都不是
选项:
正确答案:1
A和B
说明:
$ _REQUEST是预定义的超全局变量,用于在提交HTML表单后获取数据。
<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>
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.
正确答案:1
是
说明:
是的,在上面的代码中,我们使用$ _REQUEST访问输入字段的值,这是在提交HTML表单之后访问值的正确方法。
Correct answer: 1
Yes
Explanation:
The $_POST and $_GET variables are also used to get data after submitting the HTML form.
翻译自: https://www.includehelp.com/php/superglobals-aptitude-questions-and-answers.aspx