2. $GLOBALS[]
GLOBALS是由已定义全局变量组成的数组,变量名就是数组的索引,eg:
$ENTER = "<br />"; $var1 = 5; $var2 = 10; print $GLOBALS['var1'] . $ENTER ; print $GLOBALS['var2'] . $ENTER ;GLOBALS和global相似,但也有一些区别,参考:http://www.neatstudio.com/show-644-1.shtml
$ENTER = "<br />"; print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER;
参考:http://www.php.net/manual/zh/reserved.variables.server.php
4. $_GET和_POST<form action="test1.php" method="get"> Name: <input type="text" name="name_get" /> Age: <input type="text" name="age_get" /> <input type="submit" /> </form> <form action="test1.php" method="post"> Name: <input type="text" name="name_post" /> Age: <input type="text" name="age_post" /> <input type="submit" /> </form>php:
//_GET echo "name:" . $_GET["name_get"] . $ENTER ; echo "age:" . $_GET["age_get"] . $ENTER; //_POST echo "name:" . $_POST["name_post"] . $ENTER ; echo "age:" . $_POST["age_post"] . $ENTER; ?>
<form action="test1.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>PHP:
//_FILE echo "Error: " . $_FILES["file"]["error"] . "<br />"; echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"];6. _COOKIE[]
<?php setcookie("user", "zxm"); ?>读取cookie:
//cookie if (isset($_COOKIE['user'])) echo "Welcome " . $_COOKIE['user'] . "!<br />"; else echo "Welcome guest!<br />";7. _SESSION[]
<?php session_start(); $_SESSION['id'] = "100"; ?>php:
//session if(isset($_SESSION['id'])) echo "session id: " . $_SESSION['id'] . $ENTER; else echo "without session" . $ENTER; ?>7. _ENV[]
//request echo "request: " . $_REQUEST['name_get'] . "!<br />"; ?>8. 测试PHP程序:
<?php setcookie("user", "zxm"); session_start(); $_SESSION['id'] = "100"; ?> <html> <body> <?php $ENTER = "<br />"; //__GLOBALS[] $var1 = 5; $var2 = 10; print $GLOBALS['var1'] . $ENTER ; print $GLOBALS['var2'] . $ENTER ; //_SERVER print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER; //_GET echo "name:" . $_GET["name_get"] . $ENTER ; echo "age:" . $_GET["age_get"] . $ENTER; //_POST echo "name:" . $_POST["name_post"] . $ENTER ; echo "age:" . $_POST["age_post"] . $ENTER; //_FILE echo "Error: " . $_FILES["file"]["error"] . $ENTER; echo "Upload: " . $_FILES["file"]["name"] . $ENTER; echo "Type: " . $_FILES["file"]["type"] . $ENTER; echo "Size: " . ($_FILES["file"]["size"] / 1024) . $ENTER; echo "Stored in: " . $_FILES["file"]["tmp_name"] . $ENTER; //cookie if (isset($_COOKIE['user'])) echo "Welcome: " . $_COOKIE['user'] . "!<br />"; else echo "Welcome guest!<br />"; //session if(isset($_SESSION['id'])) echo "session id: " . $_SESSION['id'] . $ENTER; else echo "without session" . $ENTER; //request echo "request: " . $_REQUEST['name_get'] . "!<br />"; ?> </body> </html>