国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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 
//定義兩個全局變量,函數(shù)內(nèi)部不可以訪問
$x = 75; 
$y = 25;
//定義函數(shù)
function addition() 
{ 
//將全局變量變?yōu)槌壢肿兞浚@樣在函數(shù)內(nèi)部就可以正常訪問了
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
//調(diào)用函數(shù)
addition();
//輸出函數(shù)內(nèi)部定義的全局變量 
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 
//輸出當(dāng)前腳步的文件名
echo "<h3>輸出當(dāng)前腳步的文件名</h3>";
echo $_SERVER['PHP_SELF'];
echo "<hr/>";
//當(dāng)前腳步所在服務(wù)器的主機名
echo "<h3>當(dāng)前腳步所在服務(wù)器的主機名</h3>";
echo $_SERVER['SERVER_NAME'];
echo "<hr/>";
//當(dāng)前請求頭中 Host
echo "<h3>當(dāng)前請求頭中 Host</h3>";
echo $_SERVER['HTTP_HOST'];
echo "<hr/>";
//引導(dǎo)用戶代理到當(dāng)前頁的前一頁的地址(如果存在)
echo "<h3>引導(dǎo)用戶代理到當(dāng)前頁的前一頁的地址(如果存在)</h3>";
echo $_SERVER['HTTP_REFERER'];
echo "<hr/>";
//用來檢查瀏覽頁面的訪問者在用什么操作系統(tǒng)
echo "<h3>用來檢查瀏覽頁面的訪問者在用什么操作系統(tǒng)</h3>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<hr/>";
//包含當(dāng)前腳本的路徑
echo "<h3>包含當(dāng)前腳本的路徑</h3>";
echo $_SERVER['SCRIPT_NAME'];
?>

More important elements in the $_SERVER variable are shown in the table below:

16.png


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>
<!--創(chuàng)建個連接,連接到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>
<!-- 定義一個表單,提交一個值至當(dāng)前頁面 -->
<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>
<!-- 定義一個表單,提交一個值至當(dāng)前頁面 -->
<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

Continuing Learning
||
<?php //定義兩個全局變量,函數(shù)內(nèi)部不可以訪問 $x = 75; $y = 25; //定義函數(shù) function addition() { //將全局變量變?yōu)槌壢肿兞浚@樣在函數(shù)內(nèi)部就可以正常訪問了 $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } //調(diào)用函數(shù) addition(); //輸出函數(shù)內(nèi)部定義的全局變量 echo $z; ?>
submitReset Code