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
<?php //定义两个全局变量,函数内部不可以访问 $x = 75; $y = 25; //定义函数 function addition() { //将全局变量变为超级全局变量,这样在函数内部就可以正常访问了 $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } //调用函数 addition(); //输出函数内部定义的全局变量 echo $z; ?>
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:
<?php //输出当前脚步的文件名 echo "<h3>输出当前脚步的文件名</h3>"; echo $_SERVER['PHP_SELF']; echo "<hr/>"; //当前脚步所在服务器的主机名 echo "<h3>当前脚步所在服务器的主机名</h3>"; echo $_SERVER['SERVER_NAME']; echo "<hr/>"; //当前请求头中 Host echo "<h3>当前请求头中 Host</h3>"; echo $_SERVER['HTTP_HOST']; echo "<hr/>"; //引导用户代理到当前页的前一页的地址(如果存在) echo "<h3>引导用户代理到当前页的前一页的地址(如果存在)</h3>"; echo $_SERVER['HTTP_REFERER']; echo "<hr/>"; //用来检查浏览页面的访问者在用什么操作系统 echo "<h3>用来检查浏览页面的访问者在用什么操作系统</h3>"; echo $_SERVER['HTTP_USER_AGENT']; echo "<hr/>"; //包含当前脚本的路径 echo "<h3>包含当前脚本的路径</h3>"; 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:
<html> <body> <!--创建个连接,连接到12_7.php,并且传过去两个值--> <a href="test.php?subject=PHP&web=php.cn">点击,利用GET方式传值</a> </body> </html>
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:
<html> <body> <!-- 接收12_6.php传来的值 --> <?php echo "Study " . $_GET['subject'] . " at " . $_GET['web']; ?> </body> </html>
##
##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 the <form> tag. In this example, we specify the file to process the form data. Use other PHP files to process the data. You can modify the specified script file name. Then, we can use the super global variable $_POST to collect the input field data in the form: The example code is as follows:<html> <body> <!-- 定义一个表单,提交一个值至当前页面 --> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> Name: <input type="text" name="fname"> <input type="submit"> </form> <!-- 利用$_POST,输出传来的值 --> <?php $name = $_POST['fname']; echo $name; ?> </body> </html>
Note: 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 <form> tag. In this example, we specify the file to process the form data if you wish. To process this data, you can modify the specified script file name. Then, we can use the super global variable $_REQUEST to collect the input field data in the form:
The code is as follows:
<html> <body> <!-- 定义一个表单,提交一个值至当前页面 --> <form method="post" action=""> Name: <input type="text" name="fname"> <input type="submit"> </form> <!-- 利用$__REQUEST,输出传来的值 --> <?php $name = $_REQUEST['fname']; echo $name; ?> </body> </html>
Note: The reason for fname being undefined is the same as $_POST above
Learning 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