Detailed explanation of PHP global variables

【Foreword】

   Here's a summary of PHP's global variables

   Introduced in PHP 4.1.0, superglobals are built-in variables that are always available in all scopes

         Many of the predefined variables in PHP are "superglobal", which means they are available in the full scope of a script. They can be accessed without doing global $variable; within a function or method. These superglobals are:

          $GLOBALS,$_SERVER,$_REQUEST,

          $_POST,$_GET,$_FILES,

          $_ENV,$_COOKIE,$_SESSION

   Here are the individual ones:

【List】

   ①$GLOBALS - refers to all variables available in the global scope;

   ②$_SERVER holds information about headers, paths, and script locations;

   ③$_REQUEST is used to collect the data submitted by the HTML form;

   ④$_POST is widely used to collect form data after submitting HTML forms with method="post", and is also often used to pass variables;

   ⑤$_GET is often used to collect form data after submitting an HTML form (method="get"), and can also collect data sent in the URL

 

【main body】

   ①$GLOBALS — refers to all variables available in the global scope

    $GLOBALS This global variable is used to access global variables anywhere in the PHP script (from functions or methods)

PHP stores all global variables in an array named $GLOBALS[index]. The name of the variable is the key of the array

<?php
$x = 75;
$y = 25;
function addition() {
  $ GLOBALS ['z'] = $ GLOBALS ['x'] + $ GLOBALS ['y'];
}
addition();
echo $z; //Since z is a variable in the $GLOBALS array, it can also be accessed outside the function
?>
   ②$_SERVER holds information about headers, paths, and script locations, for example:
<?php
echo "<br>The file name of the currently executed script is ".$_SERVER['PHP_SELF'];
?>
   Here I used the concatenation operator (.)  to concatenate two string values. The following table lists the most important elements that can be accessed in $_SERVER:
$_SERVER['PHP_SELF'] Returns the filename of the currently executing script.
$_SERVER['GATEWAY_INTERFACE'] Returns the version of the CGI specification used by the server.
$_SERVER['SERVER_ADDR'] Returns the IP address of the server where the script is currently running.
$_SERVER['SERVER_NAME'] Returns the hostname of the server where the script is currently running (eg www.w3school.com.cn).
$_SERVER['SERVER_SOFTWARE'] Returns the server identification string (eg Apache/2.2.24).
$_SERVER['SERVER_PROTOCOL'] Returns the name and version of the communication protocol when the page was requested (for example, "HTTP/1.0").
$_SERVER['REQUEST_METHOD'] Returns the request method (eg POST) used to access the page.
$_SERVER['REQUEST_TIME'] Returns the timestamp when the request started (e.g. 1577687494).
$_SERVER['QUERY_STRING'] Returns the query string if this page was accessed via the query string.
$_SERVER['HTTP_ACCEPT'] Returns the request headers from the current request.
$_SERVER['HTTP_ACCEPT_CHARSET'] 返回来自当前请求的 Accept_Charset 头( 例如 utf-8,ISO-8859-1)
$_SERVER['HTTP_HOST'] 返回来自当前请求的 Host 头。
$_SERVER['HTTP_REFERER'] 返回当前页面的完整 URL(不可靠,因为不是所有用户代理都支持)。
$_SERVER['HTTPS'] 是否通过安全 HTTP 协议查询脚本。
$_SERVER['REMOTE_ADDR'] 返回浏览当前页面的用户的 IP 地址。
$_SERVER['REMOTE_HOST'] 返回浏览当前页面的用户的主机名。
$_SERVER['REMOTE_PORT'] 返回用户机器上连接到 Web 服务器所使用的端口号。
$_SERVER['SCRIPT_FILENAME'] 返回当前执行脚本的绝对路径。
$_SERVER['SERVER_ADMIN'] 该值指明了 Apache 服务器配置文件中的 SERVER_ADMIN 参数。
$_SERVER['SERVER_PORT'] Web 服务器使用的端口。默认值为 “80”。
$_SERVER['SERVER_SIGNATURE'] 返回服务器版本和虚拟主机名。
$_SERVER['PATH_TRANSLATED'] 当前脚本所在文件系统(非文档根目录)的基本路径。
$_SERVER['SCRIPT_NAME'] 返回当前脚本的路径。
$_SERVER['SCRIPT_URI'] 返回当前页面的 URI。

 

   ③$_REQUEST 用于收集 HTML 表单提交的数据

   下面的例子展示了一个包含输入字段及提交按钮的表单。当用户通过点击提交按钮来提交表单数据时, 表单数据将发送到 <form> 标签的 action 属性中指定的脚本文件。在这个例子中,我们指定文件本身来处理表单数据。如果您需要使用其他的 PHP 文件来处理表单数据,请修改为您选择的文件名即可。然后,我们可以使用超级全局变量 $_REQUEST 来收集 input 字段的值:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php 
$name = $_REQUEST['fname']; 
echo $name; 
?>

 

   ④$_POST 广泛用于收集提交 method="post" 的 HTML 表单后的表单数据,也常用于传递变量

   下例展示一个包含输入字段和提交按钮的表单。当用户点击提交按钮来提交数据后,表单数据会发送到 <form> 标签的 action 属性中指定的文件。在本例中,我们指定文件本身来处理表单数据。如果您希望使用另一个 PHP 页面来处理表单数据,请用更改为您选择的文件名。然后,我们可以使用超全局变量 $_POST 来收集输入字段的值:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php 
$name = $_POST['fname']; 
echo $name; 
?>

 

   ⑤$_GET常用于收集提交 HTML 表单 (method="get") 之后的表单数据,也可以收集 URL 中的发送的数据

      假设我们有一张页面含有带参数的超链接:

<html>
<body>
<a href="demo.php?subject=One Piece&kind=comic">Search</a>
</body>
</html>

     When the user clicks the link, the parameters "subject" and "web" are sent to "demo.php", which can then be accessed in "demo.php" via $_GET

    The following example is the code in "demo.php":

<html>
<body>
<?php
echo "I searched for " . $_GET['subject'] . " of " . $_GET['kind'];
?>
</body>
</html>

 

 

【Summarize】

①Look at the PHP form, you can learn more about $_POST and $_GET

 

 

 

 

 

 

.

   ①$GLOBALS — refers to all variables available in the global scope

    $GLOBALS This global variable is used to access global variables anywhere in the PHP script (from functions or methods)

PHP stores all global variables in an array named $GLOBALS[index]. The name of the variable is the key of the array

<?php
$x = 75;
$y = 25;
function addition() {
  $ GLOBALS ['z'] = $ GLOBALS ['x'] + $ GLOBALS ['y'];
}
addition();
echo $z; //Since z is a variable in the $GLOBALS array, it can also be accessed outside the function
?>
   ②$_SERVER holds information about headers, paths, and script locations, for example:
<?php
echo "<br>The file name of the currently executed script is ".$_SERVER['PHP_SELF'];
?>
   Here I used the concatenation operator (.)  to concatenate two string values. The following table lists the most important elements that can be accessed in $_SERVER:

   ③$_REQUEST is used to collect the data submitted by the HTML form

   The following example shows a form with input fields and a submit button. When the user submits form data by clicking the submit button, the form data is sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file itself to handle the form data. If you need to use another PHP file to process the form data, just change it to the file name of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
$name = $_REQUEST['fname'];
echo $name;
?>

 

   ④$_POST is widely used to collect form data after submitting HTML forms with method="post", and is also often used to pass variables

   下例展示一个包含输入字段和提交按钮的表单。当用户点击提交按钮来提交数据后,表单数据会发送到 <form> 标签的 action 属性中指定的文件。在本例中,我们指定文件本身来处理表单数据。如果您希望使用另一个 PHP 页面来处理表单数据,请用更改为您选择的文件名。然后,我们可以使用超全局变量 $_POST 来收集输入字段的值:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php 
$name = $_POST['fname']; 
echo $name; 
?>

 

   ⑤$_GET常用于收集提交 HTML 表单 (method="get") 之后的表单数据,也可以收集 URL 中的发送的数据

      假设我们有一张页面含有带参数的超链接:

<html>
<body>
<a href="demo.php?subject=海贼王&kind=漫画">搜索</a>
</body>
</html>

     当用户点击链接,参数 "subject" 和 "web" 被发送到 "demo.php",然后就能够通过 $_GET 在 "demo.php" 中访问这些值了

    下面的例子是 "demo.php" 中的代码:

<html>
<body>
<?php 
echo "我搜索的是" . $_GET['subject'] . "的" . $_GET['kind'];
?>
</body>
</html>

 

 

【总结】

①看PHP表单,可以学到更多有关 $_POST 和 $_GET 的知识

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326203508&siteId=291194637