PHP development basic tutorial super global variables
Summary
The global variables mentioned in the previous chapter cannot be referenced inside the function, but the super global variables can
Super global Variables are enabled after PHP 4.1.0. They are variables that come with the PHP system and are available in the entire scope of a script.
1. PHP super global variables
Several super global variables (superglobals) are predefined in PHP, which means They are available throughout a script's scope. You can use it in functions and classes without special instructions.
PHP super global variable list:
$GLOBALS
$_SERVER
$_REQUEST
- ## $_POST ## $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
- In this chapter we will explain several commonly used super global variables, and we will introduce the remaining variables in the next few chapters.
2. PHP $GLOBALS$GLOBALS is a super global variable group of PHP, which has all the functions in a PHP script It can be accessed in all domains.
$GLOBALS is a global combined array containing all variables. The name of the variable is the key of the array.
The following example introduces how to use the super global variable $GLOBALS
The code is as follows
Note: In the above example, z is a super global variable in the $GLOBALS array. Variables can also be accessed outside the function
3. PHP $_SERVER$_SERVER is a file containing header information such as ), path, and an array of information such as script locations. The items in this array are created by the web server. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here.
The following example shows how to use the elements in $_SERVER:
The example code is as follows:
输出当前脚步的文件名"; echo $_SERVER['PHP_SELF']; echo "
"; //当前脚步所在服务器的主机名 echo "当前脚步所在服务器的主机名
"; echo $_SERVER['SERVER_NAME']; echo "
"; //当前请求头中 Host echo "当前请求头中 Host
"; echo $_SERVER['HTTP_HOST']; echo "
"; //引导用户代理到当前页的前一页的地址(如果存在) echo "引导用户代理到当前页的前一页的地址(如果存在)
"; echo $_SERVER['HTTP_REFERER']; echo "
"; //用来检查浏览页面的访问者在用什么操作系统 echo "用来检查浏览页面的访问者在用什么操作系统
"; echo $_SERVER['HTTP_USER_AGENT']; echo "
"; //包含当前脚本的路径 echo "包含当前脚本的路径
"; echo $_SERVER['SCRIPT_NAME']; ?>
More important elements in the $_SERVER variable are shown in the table below:
4. PHP $_GETPHP $_GET is widely used in collection forms Data, specify this attribute in the HTML form tag: "method="get".
$_GET can also collect data sent in the URL.
Assume we have a hyperlink containing parameters HTML page:
点击,利用GET方式传值
When the user clicks on the link "Click, use GET method to pass value", the parameters "subject" and "web" will be sent to "test.php", you can use the "test.php" file Use the $_GET variable in
The following example shows the code for the "test.php" file:
##
##5. PHP $_POST
$_POST is the same as $_GET , is used to collect form data, specify this attribute in the HTML form tag: "method="post". The following example shows a form with an input field (input) and a submit button (submit) ). When the user submits the form data by clicking the "Submit" button, the form data will be sent to the script file specified in the action attribute of theNote: Because this submission is submitted to the current page, the variable fname will be displayed as undefined after the page is loaded. Once a value is submitted, it will disappear
##6. PHP $_REQUESTPHP $_REQUEST is used to collect HTML The data submitted by the form can be collected through the two submission methods of POST and GET.
The following example shows a form with an input field (input) and a submit button (submit). When you submit form data by clicking the "Submit" button, the form data will be sent to the script file specified in the action attribute of the
Note: The reason for fname being undefined is the same as $_POST aboveLearning experience:
It is not necessary to remember all some super global variables , just check the manual when needed
The difference between $_GET, $_POST, $_REQUEST will be introduced in detail in the following chapters